Programming/C#
c# - Linq
woody.choi
2017. 1. 3. 17:44
반응형
C# - LINQ
- LINQ
- 열거형 데이터에 대한 쿼리를 수행하는 기술입니다.
class Person
{
public string Name;
public int Age;
}
List<Person> students = new List<Person>
{
new Person {Name = “Jack”, Age = 33 };
new Person {Name = “Lee”, Age = 11 };
new Person {Name = “Michael”, Age = 22 };
}
var myStudents = from p in students select p;
// 다른 표현
var yourStudents = students.Select((p) => p);
// 이름만 추출
var sNames = from p in students select p.Name;
// 익명 타입으로 생성 후 추출
var myTypes = from p in students select new { Name = p.Name };
// Where을 이용한 조건
var myPeople = from p in students where p.Age > 10 select p;
// Orderby 정렬 - asending 또는 descending 추가가능
var orderedPeople = from p in students orderby p.Age descending;
// group by
var gPeople = from p in students group new {Name = p.Name } by p.Age
foreach (var peo in gPeople)
{
Console.WriteLine(peo.Key);
foreach(var item in peo)
{
Console.WriteLine(item);
}
}
반응형