[C#] LINQ 의 join 연습~
/* from a in A
join b in B on a.xxx equals b.yyy
A 안에 a 는 B안에 b 랑 같다.
*/
equals is as same as ==
재밋어재밋어~
밑에는 연습 예제~
예제는 뇌를 자극하는 c#4.0 프로그래밍에서~ 왓어요/~
class Profile
{
public string Name { get; set; }
public int Height { get; set; }
}
class Product
{
public string Title { get; set; }
public string Star { get; set; }
}
class Program
{
static void Main(string[] args)
{
Profile[] arrProfile = {
new Profile(){Name ="정우성", Height= 186},
new Profile(){Name ="김태희", Height= 158},
new Profile(){Name ="고현정", Height=172},
new Profile(){Name = "이문세", Height=178},
new Profile(){Name = "하하", Height=171}
};
Product[] arrProduct = {
new Product(){Title = "비트", Star="정우성"},
new Product(){Title = "CF 다수", Star = "김태희"},
new Product(){Title = "아이리스", Star= "김태희"},
new Product(){Title = "모래시계", Star= "고현정"},
new Product(){Title = "Solo 예찬", Star = "이문세"}
};
var listProfile =
from profile in arrProfile
join product in arrProduct on profile.Name equals product.Star
select new
{
Name = profile.Name,
Work = product.Title,
Height = profile.Height
};
Console.WriteLine("--- 내부조인결과 ---");
foreach (var profile in listProfile)
{
Console.WriteLine("이름 : {0}, 작품 : {1}, 키 : {2}", profile.Name, profile.Work, profile.Height);
}
listProfile = from profile in arrProfile
join product in arrProduct on profile.Name equals product.Star into ps
from product in ps.DefaultIfEmpty(new Product(){Title = "그런거없어!!"})
select new
{
Name = profile.Name,
Work = product.Title,
Height = profile.Height
};
Console.WriteLine ();
Console.WriteLine("--- 외부 조인 결과 ");
foreach (var profile in listProfile)
{
Console.WriteLine("이름 :{0}, 작품 : {1}, 키 : {2}", profile.Name, profile.Work, profile.Height);
}
}
댓글
댓글 쓰기