List<Model> strList = new List<Model>() { new Model(){Id=1,Name = "张三"}, new Model(){Id=11,Name = "张三1"}, new Model(){Id=2,Name = "李四"}, new Model(){Id=3,Name = "王五"}, new Model(){Id=30,Name = "王五"}, new Model(){Id=4,Name = "赵六"}, }; //查询List对象某个字段 var names = strList.Select(t => t.Name).ToList(); //字段值合并 string mergeName = string.Join("|", names.ToArray());
//根据某个字段分组 var types = strList.GroupBy(x => x.Name) .Select(group => new Model { Name = group.Key, }).ToList();
//List中实现类似SQL 查询 select in...
List<int> whereList = new List<int>() { 1, 3 }; List<Model> list = strList.Where(x => whereList.Contains(x.Id)).ToList(); foreach (var model in list) { Console.WriteLine("Id:" + model.Id + "Name:" + model.Name); } Console.WriteLine("list count==" + list.Count);
//更改list中某个字段 list.ForEach(o => o.Age = 33); list.All(o => { o.Age = 22; return true; });