๋ ๊ฐ์ from
์ ์ ์ด์ฉํ LINQ ๋ฌธ
using System.Collections;
namespace LINQEx
{
class MemberScore
{
public string Name { get; set; }
public int[] Score { get; set; }
}
class Program
{
static void Main(string[] args)
{
MemberScore[] ms =
{
new MemberScore(){Name="ํ๊ธธ๋",Score=new int[]{30, 42, 67 } },
new MemberScore(){Name="์ฒญ๊ธธ๋",Score=new int[]{88, 45, 87 } },
new MemberScore(){Name="๋
น๊ธธ๋",Score=new int[]{55, 66, 77 } },
new MemberScore(){Name="ํ๊ธธ๋",Score=new int[]{90, 80, 70 } },
new MemberScore(){Name="๋ฐฑ๊ธธ๋",Score=new int[]{99, 88, 30 } }
};
var members = from m in ms
from s in m.Score
where s < 60
orderby s, m.Name
select new {m.Name, FScore = s};
foreach (var m in members)
{
Console.WriteLine($"์ด๋ฆ : {m.Name} Fํ์ ์ฑ์ : {m.FScore}");
}
}
}
}
Output
์ด๋ฆ : ๋ฐฑ๊ธธ๋ Fํ์ ์ฑ์ : 30
์ด๋ฆ : ํ๊ธธ๋ Fํ์ ์ฑ์ : 30
์ด๋ฆ : ํ๊ธธ๋ Fํ์ ์ฑ์ : 42
์ด๋ฆ : ์ฒญ๊ธธ๋ Fํ์ ์ฑ์ : 45
์ด๋ฆ : ๋
น๊ธธ๋ Fํ์ ์ฑ์ : 55
- LINQ ๋ฌธ ๋ด ๋๋ฒ์งธ from์
from s in m.Score
๋์์ ๊ฐ ์ธ์คํด์ค ๋ณ Score๋ฐฐ์ด๋ด์์ ํ๊ฐ์ ์ ์์ฉ ํ์ธ์ ์งํํจ.
Group By ์
group by ํ์
group X by Y into Z
X๋ from ์ ์์ ๊ฐ์ ธ์จ ๋ฒ์๋ณ์
Y๋ ๋ถ๋ฅ๊ธฐ์ค
Z๋ ๊ทธ๋ฃน๋ณ์
Group By ์ ์ฌ์ฉ์์ ์ฝ๋
using System.Collections;
namespace LINQEx
{
class Member
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
Member[] m =
{
new Member(){Name = "๊ฐํธ๋", Age = 42},
new Member(){Name = "์ํฅ๋ฏผ", Age = 31},
new Member(){Name = "๊น์ฐ์", Age = 22},
new Member(){Name = "์ฅ๋ฏธ๋", Age = 35},
new Member(){Name = "์ด๋ง๊ธฐ", Age = 49}
};
var Group = from member in m
group member by member.Age > 35 into OB
select new { groupkey = OB.Key, members = OB };
foreach (var member in Group)
{
if(member.groupkey)
{
Console.WriteLine("<35์ธ ์ด๊ณผ ๋ฉค๋ฒ>");
foreach(var member2 in member.members)
{
Console.WriteLine($"์ด๋ฆ : {member2.Name} ๋์ด : {member2.Age}");
}
}
else
{
Console.WriteLine("<35์ธ ์ดํ ๋ฉค๋ฒ>");
foreach (var member2 in member.members)
{
Console.WriteLine($"์ด๋ฆ : {member2.Name} ๋์ด : {member2.Age}");
}
}
}
}
}
}
Output
<35์ธ ์ด๊ณผ ๋ฉค๋ฒ>
์ด๋ฆ : ๊ฐํธ๋ ๋์ด : 42
์ด๋ฆ : ์ด๋ง๊ธฐ ๋์ด : 49
<35์ธ ์ดํ ๋ฉค๋ฒ>
์ด๋ฆ : ์ํฅ๋ฏผ ๋์ด : 31
์ด๋ฆ : ๊น์ฐ์ ๋์ด : 22
์ด๋ฆ : ์ฅ๋ฏธ๋ ๋์ด : 35
[ํด๋น ์์ ์ฝ๋ LINQ ๊ตฌ๋ฌธ]
var Group = from member in m
group member by member.Age > 35 into OB
select new { groupkey = OB.Key, members = OB };
- 35์ธ ์ด๊ณผ์ธ ๊ฒฝ์ฐ๋ฅผ OB๋ก ๊ทธ๋ฃนํ์ ์งํ.
- OB์ ํฌํจ๋ ๋ฐ์ดํฐ๋ค์ groupkey = true ๋ก ์ ์ฅ. false ์ธ๊ฒฝ์ฐ์๋ 35์ธ์ดํ์ธ ์ธ์คํด์ค๋ค์ด ์ ์ฅ๋จ.
- ์ด๋ member ์๋ ๊ฐ๊ฐ์ ์ธ์คํด์ค๋ค์ด ํ ๋น๋จ. (๋ณ์์ฒ๋ผ ์ ์ฅ๋์ด ์ฌ์ฉํ ์ ์๋ ํํ๊ฐ ์๋ = LINQ๋ฌธ ๋ด์์๋ง ์ ํจ)
key |
data |
true |
{Name = "๊ฐํธ๋", Age = 42}, {Name = "์ด๋ง๊ธฐ", Age = 49} |
false |
{Name = "์ํฅ๋ฏผ", Age = 31}, {Name = "๊น์ฐ์", Age = 22},{Name = "์ฅ๋ฏธ๋", Age = 35} |
์กฐ์ธ(Join)
์กฐ์ธ(join)
: ์๋ก ๋ค๋ฅธ ๋๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ํฉ์น๋ ๊ฒ, ์์ ํ ๋ค๋ฅธ๊ฒ์ ์๋๊ณ ์ผ์นํ๋ ๋ถ๋ถ์ ๋ํด์ ํฉ์นจ
- `๋ด๋ถ์กฐ์ธ(inner join)` : ๋ ๋ฐ์ดํฐ์ ์ผ์นํ๋ ๋ฐ์ดํฐ๋ค๋ง ํฉ์ณ์ ๋ฐํํ๋ ๊ฒ.
- `์ธ๋ถ์กฐ์ธ(outer join)` : ๊ธฐ๋ณธ์ ์ผ๋ก ๋ด๋ถ ์กฐ์ธ๊ณผ ๋น์ทํ์ง๋ง, ์กฐ๊ฑด์ด ์ผ์นํ์ง ์๋๋ผ๋ ๊ธฐ์ค๋ฐ์ดํฐ๋ฅผ ํ๋๋ ๋๋ฝ์ํค์ง ์๊ณ , ๊ทธ๋๋ก ์ถ์ถํ์ฌ ๋น ๋ฐ์ดํฐ๋ฅผ ์ฑ์์ ํตํฉํ๋ ๋ฐฉ์
- ์ธ๋ถ์กฐ์ธ์์ ๊ธฐ์ค๋ฐ์ดํฐ๊ฐ ์ด๋ค๊ฒ์ด๋์ ๋ฐ๋ผ ๋ง๋ถ๋ ๋ฐ์ดํฐ๊ฐ ๋ฌ๋ผ์ง.
- ๊ธฐ์ค๋ฐ์ดํฐ์ ๋จ์ ์ผ์นํ์ง ์์ ๋ฐ์ดํฐ์ ์ผ์นํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋๊ฒ.
- ์ฌ๊ธฐ์ ๋์จ ๊ฐ๋
์ด `์ผ์ชฝ์กฐ์ธ(left join)`, `์ค๋ฅธ์ชฝ์กฐ์ธ(right join)`, `์์ ์กฐ์ธ(full-outer join)`์ด๋ค.
LINQ
์์๋ ์ผ์ชฝ ์กฐ์ธ(left join)๋ง์ ์ง์ํ๊ณ , ์ ๋งํด์๋ ์ผ์ชฝ์กฐ์ธ๋ง์ผ๋ก ํด๊ฒฐ๊ฐ๋ฅํ๋ค.
์กฐ์ธ(join) ์ ์ธ ํ์
from a in A // ๊ธฐ์ค๋ฐ์ดํฐ
join b in B on a.xxx equals b.xxx // ์กฐ์ธ๋ฐ์ดํฐ ๋ฐ ์กฐ์ธ์ ์ธ
๋ด๋ถ์กฐ์ธ(Inner Join) ์ฌ์ฉ ์ฝ๋ ์์
using System.Collections;
namespace LINQEx
{
class Student
{
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)
{
Student[] studentList =
{
new Student(){name = "๊น๋ง๋ฅ", age = 21},
new Student(){name = "๋๋ง๋ฅ", age = 22},
new Student(){name = "๋ฐ๋ง๋ฅ", age = 23},
new Student(){name = "์ด๋ง๋ฅ", age = 24},
new Student(){name = "์ต๋ง๋ฅ", age = 25},
};
Score[] scoreList =
{
new Score(){name = "๊น๋ง๋ฅ", math = 98, english =77},
new Score(){name = "๋๋ง๋ฅ", math = 88, english =66},
new Score(){name = "๊ฐ๋ง๋ฅ", math = 69, english =99},
new Score(){name = "์ด๋ง๋ฅ", math = 79, english =88},
new Score(){name = "์ค๋ง๋ฅ", math = 54, english =55},
};
var Students = from student in studentList
join score in scoreList on student.name equals score.name
select new
{
Name = student.name,
Age = student.age,
Math = score.math,
English = score.english,
};
foreach (var s in Students)
{
Console.WriteLine($"์ด๋ฆ : {s.Name}, ๋์ด : {s.Age}์ธ, ์ํ : {s.Math}์ , ์์ด : {s.English}์ ");
}
}
}
}
Output
์ด๋ฆ : ๊น๋ง๋ฅ, ๋์ด : 21์ธ, ์ํ : 98์ , ์์ด : 77์
์ด๋ฆ : ๋๋ง๋ฅ, ๋์ด : 22์ธ, ์ํ : 88์ , ์์ด : 66์
์ด๋ฆ : ์ด๋ง๋ฅ, ๋์ด : 24์ธ, ์ํ : 79์ , ์์ด : 88์
Student ์ธ์คํด์ค
์ Score ์ธ์คํด์ค
๋ฐ์ดํฐ ๋ด ์ผ์นํ๋ ์ด๋ฆ์ด ๊น๋ง๋ฅ, ๋๋ง๋ฅ, ์ด๋ง๋ฅ์ธ๊ฒ์ ํ์ธํ ์ ์๋ค.
- ๊ธฐ์ค๋ฐ์ดํฐ๋
Student ์ธ์คํด์ค ๋ด ๋ฐ์ดํฐ
๋ก ํ์์ผ๋ฉฐ, ๋ด๋ถ์กฐ์ธ
์ด๋ฏ๋ก ์ผ์นํ๋ ๋ฐ์ดํฐ๋ง์ ๋ฐํํ๋ค.
์ธ๋ถ์กฐ์ธ(Outer Join)
์ธ๋ถ์กฐ์ธ ํ์
from a in A
join b in B on a.xxx equals b.xxx into temp
// ์ฌ๊ธฐ๊น์ง๋ ๋ด๋ถ์กฐ์ธ๊ณผ ๋น์ทํ ๋์์ํด์ ๋ฐํ๊ฒฐ๊ณผ๋ฅผ temp์ ์ ์ฅ
from b in temp.DefaultEmpty(new B() {empty = ""})
// temp์์ ๋น์ด์๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ b์์์ ์์ฑ๊ฐ์ผ๋ก ์๋ก์ด ๊ฐ์ฒด๋ฅผ ํตํด ์์ฑ.
์ธ๋ถ์กฐ์ธ(Outer Join) ์ฌ์ฉ ์ฝ๋ ์์
using System.Collections;
namespace LINQEx
{
class Student
{
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)
{
Student[] studentList =
{
new Student(){name = "๊น๋ง๋ฅ", age = 21},
new Student(){name = "๋๋ง๋ฅ", age = 22},
new Student(){name = "๋ฐ๋ง๋ฅ", age = 23},
new Student(){name = "์ด๋ง๋ฅ", age = 24},
new Student(){name = "์ต๋ง๋ฅ", age = 25},
};
Score[] scoreList =
{
new Score(){name = "๊น๋ง๋ฅ", math = 98, english =77},
new Score(){name = "๋๋ง๋ฅ", math = 88, english =66},
new Score(){name = "๊ฐ๋ง๋ฅ", math = 69, english =99},
new Score(){name = "์ด๋ง๋ฅ", math = 79, english =88},
new Score(){name = "์ค๋ง๋ฅ", math = 54, english =55},
};
var Students = from student in studentList
join score in scoreList on student.name equals score.name into temp
from score in temp.DefaultIfEmpty(new Score() { math = 100, english = 100 })
select new
{
name = student.name,
age = student.age,
math = score.math,
english = score.english,
};
foreach (var student in Students)
{
Console.WriteLine(student);
}
}
}
}
Output
{ name = ๊น๋ง๋ฅ, age = 21, math = 98, english = 77 }
{ name = ๋๋ง๋ฅ, age = 22, math = 88, english = 66 }
{ name = ๋ฐ๋ง๋ฅ, age = 23, math = 100, english = 100 }
{ name = ์ด๋ง๋ฅ, age = 24, math = 79, english = 88 }
{ name = ์ต๋ง๋ฅ, age = 25, math = 100, english = 100 }