namespace IEnumreable
{
class Person
{
public string Name { get; set; }
public string Sex { get; set; }
public string Desption { get; set; }
public int DID { get; set; }
}
class Department
{
public int ID { get; set; }
public string Name { get; set; }
}
class Program
{
private static void Main(string[] args)
{
IEnumerable<Person> personlist = new List<Person>()
{
new Person {Name = "liuming", Sex = "man", Desption = "chianese",DID=1},
new Person {Name = "liumingfeng", Sex = "man", Desption = "chianese",DID=2},
new Person {Name = "liuminglove", Sex = "man", Desption = "chianese",DID=2},
new Person {Name = "liuming1", Sex = "man", Desption = "chianese",DID=1},
new Person {Name = "liumingdd", Sex = "man", Desption = "chianese",DID=3}
};
IEnumerable<Department> delist = new List<Department>()
{
new Department(){ID = 1,Name = "de1"},
new Department(){ID = 2,Name = "de2"},
new Department(){ID = 3,Name = "de3"}
};
//where就像SQl里面的where一样对集合进行筛选
var whered = personlist.Where(o => o.Name.Contains("feng"));
//select就像sql里面的select一样对需要的属性进行映射
var selected1 = personlist.Select(o => o.Name);
var selected2 = personlist.Select(o => new { o.Name, Desc = o.Desption });
//count方法对元素数量进行计数
int totile = personlist.Count();
int sub = personlist.Count(o => o.Name.StartsWith("liu"));
//orderby和thenby排序
var ordered = personlist.OrderBy(o => o.Name).ThenBy(o => o.Sex);
var ordered2 = personlist.OrderByDescending(o => o.Name).ThenByDescending(o => o.Sex);
//join链接查询
var joined = personlist.Join(delist, p => p.DID, d => d.ID, (p, d) => new { p.Name, p.Sex, p.DID, dname = d.Name });
//groupby分组结果
var grouped = personlist.GroupBy(o => o.DID);
foreach (var item in grouped)
{
foreach (var i in item)
{
Console.WriteLine(i.Name);
}
}
//实现多对一的join使用groupjoin
var groupjoined = delist.GroupJoin(personlist, d => d.ID, p => p.DID, (de, pes) => new { de.ID, de.Name, Persons = pes });
Console.ReadKey();
}
}
}