LInq 使用where过滤,匿名类型来查找想要的数据。
View Code
static void Main(string[] args)
{
List<Person> personL = new List<Person>();
personL.Add(new Person("wtq", "男", "13616009873", 23));
personL.Add(new Person("wta", "女", "13616009871", 24));
personL.Add(new Person("wtb", "男", "13616009872", 26));
personL.Add(new Person("wtc", "男", "13616009873", 25));
personL.Add(new Person("wtd", "男", "13616009874", 27));
personL.Add(new Person("wte", "女", "13616009875", 28));
personL.Add(new Person("wtf", "男", "13616009876", 29));
personL.Add(new Person("wtg", "男", "13616009877", 30));
personL.Add(new Person("wth", "女", "13616009878", 23));
var pr = from n in personL
where n.Age == 23
select new { myname = n.Name, mysex = n.Sex };
foreach (var item in pr)
{
Console.WriteLine(item.myname+" "+item.mysex);
}
Console.ReadLine();
}
{
List<Person> personL = new List<Person>();
personL.Add(new Person("wtq", "男", "13616009873", 23));
personL.Add(new Person("wta", "女", "13616009871", 24));
personL.Add(new Person("wtb", "男", "13616009872", 26));
personL.Add(new Person("wtc", "男", "13616009873", 25));
personL.Add(new Person("wtd", "男", "13616009874", 27));
personL.Add(new Person("wte", "女", "13616009875", 28));
personL.Add(new Person("wtf", "男", "13616009876", 29));
personL.Add(new Person("wtg", "男", "13616009877", 30));
personL.Add(new Person("wth", "女", "13616009878", 23));
var pr = from n in personL
where n.Age == 23
select new { myname = n.Name, mysex = n.Sex };
foreach (var item in pr)
{
Console.WriteLine(item.myname+" "+item.mysex);
}
Console.ReadLine();
}
使用foreach进行遍历数据。
在使用匿名类型时,可以不指定名称,而是使用元数据的字段名,如下代码:
View Code
var pr = from n in personL
where n.Age == 23
select new { n.Name, n.Age };
where n.Age == 23
select new { n.Name, n.Age };