时间:2021-07-01 10:21:17 帮助过:3人阅读
并且有一些初始化语句
List<People> PeopleList = new List<People>();
PeopleList.Add(new People() { Name = "Haocheng Wu", Age = 24 });
PeopleList.Add(new People() { Name = "Haocheng Wu", Age = 25 });
PeopleList.Add(new People() { Name = "James Wu", Age = 23 });
你就可以采用下面类似于SQL语句的方法进行select
List<string> SubPeopleNameList1 = (from people in PeopleList
where people.Name == "Haocheng Wu" && people.Age == 24
select people.Name).ToList<string>();
当然你也可以用一行代替
List<string> SubPeopleNameList2 = PeopleList.Where(people => people.Name == "Haocheng Wu" && people.Age == 24).Select(people => people.Name).ToList();
不过显然第一种方法更加一目了然,尤其是当判断条件相当复杂的时候就更加有用了
C# List中写出LINQ类似SQL的语句
标签: