一、List排序
采用自定义Sort比较器和Linq
class Persion { public string name; public int age; } //自定义Sort方法 private static void sortListClass() { Persion p1 = new Persion() { name = "huyong", age = 27 }; Persion p2 = new Persion() { name = "tansha", age = 25 }; List<Persion> listPersion = new List<Persion>(); listPersion.Add(p1); listPersion.Add(p2); listPersion.Sort(sortage); foreach (var item in listPersion) { Console.WriteLine(item.name + ":" + item.age); } } static int sortage(Persion x, Persion y) { if (x.age < y.age) return -1; //按age从大到小 降序排列 else if (x.age == y.age) return 0; else return 1; } //linq方法 private static void sortLinq() { Persion p1 = new Persion() { name = "huyong", age = 27 }; Persion p2 = new Persion() { name = "tansha", age = 25 }; Persion p3 = new Persion() { name = "tansha", age = 27 }; List<Persion> listPersion = new List<Persion>(); listPersion.Add(p1); listPersion.Add(p2); listPersion.Add(p3); var lList = from t in listPersion where (t.age == 27) orderby t.name ascending select t; foreach (var item in lList) { Console.WriteLine(item.name + ":" + item.age); } }
二、用Linq将两个list合并为一个list
//linq合并两个list,根据某一相同字段 private static void mergeList() { Persion p1 = new Persion() { name = "hy", province = "jx" }; Persion p2 = new Persion() { name = "tansha", province = "hn" }; Persion p3 = new Persion() { name = "ht", province = "gd" }; List<Persion> listPersion = new List<Persion>(); listPersion.Add(p1); listPersion.Add(p2); listPersion.Add(p3); Persion p11 = new Persion() { name = "hy", age = 23, sex = "男" }; Persion p21 = new Persion() { name = "tansha", age = 12, sex = "女" }; Persion p31 = new Persion() { name = "ht", age = 7, sex = "男" }; List<Persion> listPersion2 = new List<Persion>(); listPersion2.Add(p11); listPersion2.Add(p21); listPersion2.Add(p31); //linq合并两个list,根据某一相同字段 var list = from pp1 in listPersion join pp2 in listPersion2 on pp1.name equals pp2.name select new Persion { name = pp1.name, province = pp1.province, age = pp2.age, sex = pp2.sex }; foreach (var item in list) { Console.WriteLine(item.name + ":" + item.age + ":" + item.sex + ":" + item.province); } }
三、对List里面的某个数据进行操作并保存
lists.Where(q =>q.SellChannelId == ((int)SellChannelType.PB).ToString()).ToList().ForEach(q => q.index = 10);
四、List<T>转化为IQueryable<T>
不能直接转换,
IQueryable在数据库端中查询,
IEnumerable在内存上查询。
List<T>支持后者。可以用 list.AsQueryable<T>转换。
五、使用LINQ从List<T>集合中删除指定集合元素
实体类(Author):
public class Author{ public int id{get;set;} public string firstname{get;set;} public string lastname {get;set;} }
比如有如下的LINQ查询语句:
var authors = from x in authorsList
select x;
其中,需要从查询集合中删除 firstname=’Bob’ 的所有用户,应该如何实现呢?
方案一:
authorsList = authorsList.Where(x => x.FirstName != "Bob").ToList(); //或者 authorsList.RemoveAll(x => x.firstname == "Bob");
方案二:
var authorsList = new List<Author>() { new Author{ Firstname = "Bob", Lastname = "Smith" }, new Author{ Firstname = "Fred", Lastname = "Jones" }, new Author{ Firstname = "Brian", Lastname = "Brains" }, new Author{ Firstname = "Billy", Lastname = "TheKid" } }; var authors = authorsList.Where(a => a.Firstname == "Bob"); authorsList = authorsList.Except(authors).ToList(); authorsList = authorsList.Except(authorsList.Where(a=>a.Firstname=="Billy")).ToList();