미처 완성되지 못한 알고리즘

LINQ 본문

IT/c#

LINQ

-June- 2017. 8. 24. 00:13

LINQ는 개인적인 사견으로 간단히 얘기하면 C#에서 SQL같은 쿼리문으로 특정 데이터들의 집합을


추출해내는 문법이다.



주의: LINQ는 닷넷 프레임 3.5 이상부터 가능하다.



예)


using System.Linq;



namespace ConsoleApp1

{

    class Program

    {


        static void Main(string[] args)

        {

            int[] nums = { 1,5,76,7,3,345,76,76,467,456,34,23,23,99};


            var result = from num in nums               

                         where num % 3 == 0

                         where num % 2 == 0

                         orderby num

                         select num;


            foreach (int n in result)

                Console.WriteLine("결과 값: {0}", n );

        }

    }

}




          var result = from num in nums               

                         where num % 3 == 0

                         where num % 2 == 0

                         orderby num

                         select num;




이 부분이 LINQ이다.  SQL과 많이 흡사하다.


where절은 한 번만 써도 되며 orderby는 생략이 가능하다.


저 후에 result.any() 등 각종 함수로 값 조회 등이 가능하다.



LINQ 예제)



 class Student

    {

        public string name { get; set; }

        public int[] score { get; set; }

    }


    class Program

    {   

        static void Main(string[] args)

        {

            Student[] stdList =

             {

                new Student() { name = "민수", score = new int [] { 77, 88, 66, 99} },

                new Student() { name = "동욱", score = new int [] { 97, 67, 87, 77} },

                new Student() { name = "인나", score = new int [] { 53, 63, 73, 83} }

            };


            var Students = from student in stdList

                           from score in student.score

                           where score > 90

                           select new { name = student.name, score = score };


            Console.WriteLine("우수성적 학생");

            foreach (var student in Students)

                Console.WriteLine("이름 : {0} , 점수 : {1}", student.name, student.score);

        }//main

}





LINQ 예제2)


    class Person

    {

        public string name { get; set; }

        public string sex { get; set; }

    }


    class Program

    {   

        static void Main(string[] args)

        {

            Person[] personList =

            {

                new Person() { sex = "여자", name = "전지현"},

                new Person() { sex = "남자", name = "김동욱"},

                new Person() { sex = "여자", name = "권유리"},

                new Person() { sex = "남자", name = "유준상"},

                new Person() { sex = "여자", name = "김고은"}

            };


            var Group = from person in personList

                        group person by person.sex == "남자" into data

                        select new { checksex = data.Key, people = data };


            foreach(var group in Group)

            {

                if (group.checksex)

                {

                    Console.WriteLine("[남자 목록]");

                    foreach (var person in group.people)

                        Console.WriteLine("이름 : {0}", person.name);

                }

                else

                {

                    Console.WriteLine("[여자 목록]");

                    foreach (var person in group.people)

                        Console.WriteLine("이름 : {0}", person.name);

                }

            }

        }//main


    }//class




LINQ 예제3)



        class Profile

    {

        public string name { get; set; }

        public int age { get; set; }

    }


    class Score

    {

        public string name { get; set; }

        public int math { get; set; }

        public int english { get; set; }

    }


    class Program

    {   

        static void Main(string[] args)

        {

            Profile[] profileList =

            {

                new Profile() { name = "설현", age = 22},

                new Profile() { name = "수지", age =24},

                new Profile() { name = "아린", age =19 }

            };


            Score[] scoreList =

            {

                new June.Score() { name = "설현", math = 99, english = 89},

                new June.Score() { name = "현아", math = 77, english = 69},

                new June.Score() { name = "수지", math = 55, english = 79}

            };


            var Students = from profile in profileList

                           join score in scoreList on profile.name equals score.name

                           select new

                           {

                               name = profile.name,

                               age = profile.age,

                               math = score.math,

                               english = score.math

                           };


            foreach (var student in Students)

                Console.WriteLine(student);

        }//main


    }//class

'IT > c#' 카테고리의 다른 글

#region  (0) 2017.09.08
Thread  (0) 2017.08.24
delegate 와 event  (0) 2017.08.23
오버플로를 검사하기 위한 checked와 unchecked 문  (0) 2017.08.23
Nullable type  (0) 2017.08.23