Language INtegrated Query
데이터 질의기능
최종질의결과로 IEnumerable<\T> 반환
컬렉션을 반환한다고 볼 수 있음.
using System;
using System.Linq;
namespace Program
{
class Gorani
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
Gorani[] goranis =
{
new Gorani() {Name="a", Age=5},
new Gorani() {Name="b", Age=1},
new Gorani() {Name="c", Age=2},
new Gorani() {Name="d", Age=4},
new Gorani() {Name="e", Age=3},
};
var result_1 = from gorani in goranis
where gorani.Age > 2
orderby gorani.Age descending
select gorani;
Console.WriteLine("===result_1===");
foreach (var gorani in result_1)
Console.WriteLine($"{gorani.Name} {gorani.Age}");
Console.WriteLine();
var result_2 = from gorani in goranis
where gorani.Age < 4
orderby gorani.Name
select gorani.Name;
Console.WriteLine("===result_2===");
foreach (var name in result_2)
Console.WriteLine($"{name}");
Console.WriteLine();
var result_3 = from gorani in goranis
where gorani.Age > 0
orderby gorani.Name descending
select new { ItsName = gorani.Name, Age=gorani.Age };
Console.WriteLine("===result_3===");
foreach (var gorani in result_3)
Console.WriteLine($"{gorani.ItsName} {gorani.Age}");
Console.WriteLine();
/*
===result_1===
a 5
d 4
e 3
===result_2===
b
c
e
===result_3===
e 3
d 4
c 2
b 1
a 5
*/
}
}
}
using System;
using System.Linq;
namespace Program
{
class Gorani
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
Gorani[] goranis =
{
new Gorani() {Name="a", Age=5},
new Gorani() {Name="b", Age=1},
new Gorani() {Name="c", Age=2},
new Gorani() {Name="d", Age=4},
new Gorani() {Name="e", Age=3},
};
var result = from gorani in goranis
group gorani by gorani.Age < 3 into g
select new { Group = g.Key, Goranis = g };
foreach(var group in result)
{
Console.WriteLine($"----- {group} -----");
foreach(var gorani in group.Goranis)
Console.WriteLine($"{gorani.Name} {gorani.Age}");
}
/*
----- { Group = False, Goranis = System.Linq.Grouping`2[System.Boolean,Program.Gorani] } -----
a 5
d 4
e 3
----- { Group = True, Goranis = System.Linq.Grouping`2[System.Boolean,Program.Gorani] } -----
b 1
c 2
*/
}
}
}
두 데이터에서 일치하는 부분을 연결
using System;
using System.Linq;
namespace Program
{
class Gorani // 고라니
{
public string Name { get; set; }
public int Age { get; set; }
}
class Owner // 고라니 오-너
{
public string Name { get; set; }
public string GoraniName { get; set; }
}
class Program
{
static void Main(string[] args)
{
Gorani[] goranis =
{
new Gorani() {Name="a", Age=5},
new Gorani() {Name="b", Age=1},
new Gorani() {Name="c", Age=2},
new Gorani() {Name="d", Age=4},
new Gorani() {Name="e", Age=3},
};
Owner[] owners =
{
new Owner() {Name="X", GoraniName="a"},
new Owner() {Name="Y", GoraniName="c"},
new Owner() {Name="Y", GoraniName="d"},
new Owner() {Name="Z", GoraniName="e"},
};
var result = from gorani in goranis
join owner in owners
on gorani.Name equals owner.GoraniName
select new { PersonName = owner.Name, GoraniName = gorani.Name };
foreach(var item in result)
{
Console.WriteLine($"{item.PersonName} {item.GoraniName}");
}
/*
X a
Y c
Y d
Z e
*/
}
}
}
한 데이터를 기준으로 다른 데이터 원본과 연결
using System;
using System.Linq;
namespace Program
{
class Gorani // 고라니
{
public string Name { get; set; }
public int Age { get; set; }
}
class Owner // 고라니 오-너
{
public string Name { get; set; }
public string GoraniName { get; set; }
}
class Program
{
static void Main(string[] args)
{
Gorani[] goranis =
{
new Gorani() {Name="a", Age=5},
new Gorani() {Name="b", Age=1},
new Gorani() {Name="c", Age=2},
new Gorani() {Name="d", Age=4},
new Gorani() {Name="e", Age=3},
};
Owner[] owners =
{
new Owner() {Name="X", GoraniName="a"},
new Owner() {Name="Y", GoraniName="c"},
new Owner() {Name="Y", GoraniName="d"},
new Owner() {Name="Z", GoraniName="e"},
};
var result = from gorani in goranis
join owner in owners
on gorani.Name equals owner.GoraniName into ps
from temp_owner in ps.DefaultIfEmpty(new Owner() { Name="None" })
select new { PersonName = temp_owner.Name, GoraniName = gorani.Name };
foreach(var item in result)
Console.WriteLine($"{item.PersonName} {item.GoraniName}");
/*
X a
None b
Y c
Y d
Z e
*/
}
}
}