레이블이 LINQ인 게시물을 표시합니다. 모든 게시물 표시
레이블이 LINQ인 게시물을 표시합니다. 모든 게시물 표시

2022년 2월 19일 토요일

(C#동영상)C# LINQ 링크/링큐 Select, SelectMany 비교, 차이점 실습, 닷넷교육동영상, C#학원, 닷넷학원, 자바학원, WPF학원, 닷넷교육, C#교육. 자바교육

 (C#동영상)C# LINQ 링크/링큐 Select, SelectMany 비교, 차이점 실습, 닷넷교육동영상, C#학원, 닷넷학원, 자바학원, WPF학원, 닷넷교육, C#교육. 자바교육



http://ojc.asia/bbs/board.php?bo_table=LecCsharp&wr_id=420 


(동영상)C# LINQ 링크/링큐 Select, SelectMany 비교, 차이점 실습

(동영상)C# LINQ 링크/링큐 Select, SelectMany 비교, 차이점 실습LINQSelect, SelectMany실습C# LINQ(Select, SelectMany)LINQ에서 프로젝션 작업은 필요한 속성을 가지고 오는 것인데 Select, SelectMany를 사용한다.기본적

ojc.asia


https://www.youtube.com/watch?v=GqDWbAOjO4A&list=PLxU-iZCqT52DJyR6gqJy0MCL8RiTVXdos&index=22 



https://www.youtube.com/watch?v=Sp_WKTMGJ8U&list=PLxU-iZCqT52DJyR6gqJy0MCL8RiTVXdos&index=25


LINQ

Select, SelectMany


실습






C# LINQ(Select, SelectMany)


LINQ에서 프로젝션 작업은 필요한 속성을 가지고 오는 것인데 Select, SelectMany를 사용한다. 


기본적으로 Select는 하나의 컬렉션에서 추출하는 것이고, SelectMany는 다중 컬렉션에서 질의가 가능하다.  즉 하나의 컬렉션 안에 또 다른 SUB 컬렉션이 있을 때 SUB 컬렉션에서 데이터를 추출하는 경우에 사용한다.


 

또한 SelectMany는 SQL의 Cross Join과 같이 가능한 모든 조인 결과 집합을 만들어 낼 수 있다.


namespace SemectMany

{

class Customer

{

public string Name { get; set; }

public List<String> Cards { get; set; }

}

class Test

{

static List<Customer> Getcustomers()

{

List<Customer> customers = new List<Customer>{

new Customer {

Name = "박길동",

Cards= new List<string>{"BC","HANA", "WOORI"}},

new Customer {

Name = "김길동",

Cards= new List<string>{"BC","WOORI"}},

new Customer {

Name = "가길동",

Cards= new List<string>{"HANA","WOORI"}},

new Customer {

Name = "하길동",

Cards= new List<string>{"WOORI","BC"}}};

return customers;

}



static void Main(string[] args)

{

List<string> animals = new List<string>() { "cat", "dog", "monkey" };

List<int> number = new List<int>() { 10, 20 };


// SQL의 Cross Join과 같이 가능한 모든 조인 집합을 만듦

var mix1 = number.SelectMany(num => animals, (n, a) => new { n, a }).ToList();

mix1.ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------1");


// 다중 컬렉션 질의

var mix2 = animals.SelectMany(a => a).ToList();

mix2.ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------2");


// 단일컬렉션 질의

var animals1 = animals

.Where(n => n.StartsWith("c"))

.Select(n => n).ToList();

animals1.ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------3");


// 다중컬렉션 질의

var animals2 = animals

.Where(n => n.StartsWith("c"))

.SelectMany(n => n).ToList();

animals2.ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------4");


// customers에서 이름이 '가'로 시작되는 고객의 이름출력

var result1 = from c in Getcustomers()

  where c.Name.StartsWith("가")

  select c.Name;

result1.ToList().ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------5");


//new로 만들어내는 무명개체에서 Name추출

var result2 = from c in Getcustomers()

  where c.Name.StartsWith("가")

  select new { c.Name };

result2.ToList().ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------6");


//Select로 Customer의 Card를 출력, foreach문 2개필요

//바깥쪽 반복문에서 개인별 카드리스트를 출력

//안쪽 반복문에서 카드명을 출력

var result3 = Getcustomers().Select(c => c.Cards);

foreach (List<String> cardList in result3)

{

foreach (string card in cardList)

{

Console.WriteLine(card);

}

}

Console.WriteLine("-------------7");


// 메소드기반 쿼리식에서 SelectMany를 직접사용

// 다중 컬렉션 질의

var result4 = Getcustomers().SelectMany(c => c.Cards);

result4.ToList().ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------8");


// 메소드기반 쿼리식에서 Where를 이용하여 BC카드를 가진 Customer의 Name을 출력

var result5 = Getcustomers()

  .Where(c => c.Cards.Contains("BC"))

  .Select(c => c.Name);

result5.ToList().ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------9");


// 메소드기반 쿼리식에서 Customer.Name별 Card를 출력

var result6 = Getcustomers()

.SelectMany(cust => cust.Cards,

  (cust, card) => new { cust.Name, card });

result6.ToList().ForEach(n => Console.WriteLine(n));

Console.WriteLine("-------------10");

}

}

}



#LINQ, #링크, #링큐, #Select, #SelectMany, #닷넷교육, #시샵교육, #닷넷동영상, #시샵동영상, LINQ, 링크, 링큐, Select, SelectMany, 닷넷교육, 시샵교육, 닷넷동영상, 시샵동영상,  

2022년 2월 11일 금요일

C#교육동영상, LINQ 내부조인, 외부조인 내부 JOIN을 메서드 기반으로, C#교육, 자바교육, WPF교육, 자바학원, 스프링교육, JAVA학원, JAVA동영상, 시샵동영상

 C#교육동영상,  LINQ 내부조인, 외부조인  내부 JOIN을 메서드 기반으로, C#교육, 자바교육, WPF교육, 자바학원, 스프링교육, JAVA학원, JAVA동영상, 시샵동영상


http://ojc.asia/bbs/board.php?bo_table=LecCsharp&wr_id=418 


C# LINQ 내부조인, 외부조인 내부 JOIN을 메서드 기반으로

C# LINQ 내부조인, 외부조인 내부 JOIN을 메서드 기반으로LINQ내부조인, 외부조인내부 JOIN을 메서드 기반으로C# LINQ 조인(Join)조인은 두 데이터 소스에서 조인키를 기준으로 일치하는지 비교하여 새

ojc.asia


https://www.youtube.com/watch?v=DaekIbRnM_w&list=PLxU-iZCqT52DJyR6gqJy0MCL8RiTVXdos&index=24
 

LINQ

내부조인, 외부조인

내부 JOIN을 메서드 기반으로






C# LINQ 조인(Join)


조인은 두 데이터 소스에서 조인키를 기준으로 일치하는지 비교하여 새로운 데이터 셋을 만들어 내는 것이다.




1. 내부조인 


내부조인은 교집합과 비슷하며 첫 번째 데이터원본의 특정 필드와 두 번째 데이터원본의 특정 필드를 비교하여 일치하는 데이터만 반환한다. SQL의 Inner Join과 유사하다.


[형식]


from a in 고객

join b in 매출 on a.이름 equals b.이름 

select a


on절의 조인조건은 동등비교(equals)만 가능하다.

조건에 정확히 일치하지 않는 데이터는 빠진다.


2. 외부조인


LINQ는 외부조인 중 왼쪽조인(Left Outer Join)만 지원한다. 먼저 조인절을 이용하여 조인수행 후

결과를 임시 컬렉션에 저장하고 이 임시컬렉션에 대해 DefaultIfEmpty 연산을 수행해서 비어있는 조인결과에 빈 값을 채운다. DefaultIfEmpty 연산을 거친 임시 컬렉션에서 from 절을 통해 변수를 뽑아내고 비 범위변수와 기준데이터 원본에서 뽑아낸 범위변수를 이용하여 결과를 추출한다.


namespace LinkJoin

{

    class Customer

    {

        public string Name { get; set; }

        public int Age { get; set; }

    }


    class Sale

    {

        public string Name { get; set; }

        public string Product { get; set; }

    }


    class Test

    {

        static void Main(string[] args)

        {

            Customer[] customer = {

                new Customer() {Name="홍길동", Age=8},

                new Customer() {Name="김길동",  Age=6},

                new Customer() {Name="오자커",  Age=13}

            };


            Sale[] sale = {

                new Sale() {Name="홍길동", Product="볼펜"},

                new Sale() {Name="김길동", Product="연필"},

            };


            // 고객별 매출현황을 출력

            // 내부조인("오자커"는 매출내역이 없으므로 출력되지 않는다)

            var result1 = from c in customer

                              join s in sale on c.Name equals s.Name

                             select new

                             {

                                Name = c.Name,

                                Age = c.Age,

                                 Product = s.Product

                             };


            foreach (var x in result1)  Console.WriteLine("이름 : {0}, 나이 : {1}, 상품 : {2}", x.Name, x.Age, x.Product);


            //외부조인

            result1 = from c in customer

                         join s in sale on c.Name equals s.Name into tmp

                         from s in tmp.DefaultIfEmpty(new Sale() { Product = "제품없음" })

                         select new

                         {

                             Name = c.Name,

                             Age = c.Age,

                             Product = s.Product

                         };


            Console.WriteLine("------------------------------------------------");

            foreach (var x in result1)

            {

                Console.WriteLine("이름 : {0}, 나이 : {1}, 제품 : {2}", x.Name, x.Age, x.Product);

            }


            // 내부JOIN을 메소드 쿼리식

            int[] arr1 = { 5, 6, 7};

            int[] arr2 = { 6, 7, 10 };


            Console.WriteLine("\n------------------------------------------------");

            // 메소드 기반 쿼리식

            //arr1의 수를 1증가 한값이 arr2와 같은 arr1의 값을 출력

            var res1 = arr1.Join<int, int, int, int>(arr2,

                                                       x => x + 1,   //outer key selector, 조인조건

                                                       y => y,         //inner key selector, 조인조건

                                                       (x, y) => x);   //결과 : 조인을 만족하는 X값을 출력

            foreach (var r in res1) Console.Write(r + " ");


            Console.WriteLine("\n------------------------------------------------");

            //쿼리식 기반

            //arr1의 수를 1증가 한값이 arr2와 같은 arr1의 값을 출력

            var res2 = from x in arr1

                           join y in arr2 on (x + 1) equals y

                          select x;


            foreach (var r in res2) Console.Write(r + " ");

            Console.WriteLine("\n------------------------------------------------");

        }

    }

}




[결과]

이름 : 홍길동, 나이 : 8, 상품 : 볼펜

이름 : 김길동, 나이 : 6, 상품 : 연필

------------------------------------------------

이름 : 홍길동, 나이 : 8, 제품 : 볼펜

이름 : 김길동, 나이 : 6, 제품 : 연필

이름 : 오자커, 나이 : 13, 제품 : 상품없음


-------------

5 6

-------------

5 6

-------------



#LINQ, #내부조인, #외부조인, #LINQJOIN, #LINQ조인, #시샵교육, #시샵동영상, #닷넷교육, #닷넷동영상, #시샵학원, #닷넷학원, LINQ, 내부조인, 외부조인, LINQJOIN, LINQ조인, 시샵교육, 시샵동영상, 닷넷교육, 닷넷동영상, 시샵학원, 닷넷학원,  

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...