List有集成了很多方法,如果在一个list中,需要选择仅仅需要的字段,或者筛选出满足条件的对象,可以参考此种用法:
namespace TestDemo { public class Program { static void Main(string[] args) { List<Person> perList = new List<Person>() { new Person(){Id=1,Name="xqq",Age=27,Description="This is a Test"}, new Person(){Id=1,Name="hjj",Age=26,Description="This is a Test"}, new Person(){Id=1,Name="zcy",Age=26,Description="This is a Test"}, new Person(){Id=1,Name="lxq",Age=25,Description="This is a Test"} }; //List Select仅仅是选中此List的相关属性(name,age...) perList.Select(p => p.Name).ToList().ForEach(r => Console.WriteLine(r)); //List Select选择多个属性 perList.Select(p => new { p.Name, p.Age }).ToList().ForEach(r=>Console.WriteLine(r)); //List 选择name=xqq的对象的(name,age及description属性) perList.Select(p => new { p.Name, p.Age, p.Description }).Where(p => p.Name == "xqq").ToList().ForEach(r=>Console.WriteLine(r)); //查询的返回第一条记录 Console.WriteLine(perList.Select(p => new { p.Name }).ToList().Find(d => d.Name.Contains("q")).ToString()); //查询返回所有的记录 perList.Select(p => new { p.Name, p.Age }).ToList().FindAll(d => d.Name.Contains("q")).ForEach(r=>Console.WriteLine(r)); Console.ReadKey(true); } } public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Description { get; set; } } }
仅自己参考