利用Linq查询List中数据的示例

.Net技术 码拜 10年前 (2015-01-21) 2563次浏览 0个评论

//1,查询出list中所有女生并且年龄小于18,并按降序排列
//2,查询出list中名字为“王”开头,并且长度为3的学生
using System;
using System.Collections.Generic;
using System.Linq;
public class StudyLinq
{
public static void Main()
{
List<People> list = new List<People>();
list.Add(new People(){Name = “李世民”,Age = 18 ,Sex = “男”});
list.Add(new People(){Name = “李渊”,Age = 20 ,Sex = “男”});
list.Add(new People(){Name = “李隆基”,Age = 18 ,Sex = “男”});
list.Add(new People(){Name = “王羲之”,Age = 23 ,Sex = “男”});
list.Add(new People(){Name = “武则天”,Age = 17 ,Sex = “女”});
list.Add(new People(){Name = “慈禧太后”,Age = 16 ,Sex = “女”});
var result = list.Where (p=>p.Age<18 && p.Sex ==”女”).OrderByDescending(p=>p.Age);
foreach (People a in result)
{
Console.WriteLine(a.Name + ” “+a.Age);
}
var result1 = list.Where (p=>p.Name.StartsWith (“王”) && p.Name.Length == 3);
foreach (People b in result1)
{
Console.WriteLine(b.Name);
}
}
public class People
{
public string Name
{
get;set;
}
public int Age
{
get;set;
}
public string Sex
{
get;set;
}
}
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明利用Linq查询List中数据的示例
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!