A, List Sort
Sort using custom comparator and 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); } }
Second, with the two Linq into one list list
// LINQ two combined list, according to a same field Private static void mergeList () { persion P1 = new new persion () {name = " HY " , Province = " JX " }; persion P2 = new new persion () {name = " tansha " , Province = " HN " }; persion P3 = new 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); } }
Third, on the List a data inside the operation and save
lists.Where(q =>q.SellChannelId == ((int)SellChannelType.PB).ToString()).ToList().ForEach(q => q.index = 10);
Four, List <T> into IQueryable <T>
It can not be directly converted,
IQueryable query the database side,
IEnumerable query in memory.
List <T> to support the latter. Can be converted with list.AsQueryable <T>.