//Linq查询 List<A1> a1 = new List<A1>(); a1.Add(new A1 { Id = 10, Name = "aaa", Age = 101, Gender = true }); a1.Add(new A1 { Id = 15, Name = "bbb", Age = 21, Gender = true }); a1.Add(new A1 { Id = 12, Name = "ccc", Age = 21, Gender = false }); a1.Add(new A1 { Id = 13, Name = "ddd", Age = 200, Gender = false }); List<DogToy> a2 = new List<DogToy>(); a2.Add(new DogToy { DogId = 11, DogName = "小白" }); a2.Add(new DogToy { DogId = 10, DogName = "小欢" }); a2.Add(new DogToy { DogId = 12, DogName = "小美" }); a2.Add(new DogToy { DogId = 15, DogName = "小王" }); //查询所有语句 var s1 = (from c in a1 select c).ToList(); //查询Gender为false的成员 var s2 = from c in a1 where c.Gender == false select c; //orderby排序 var s3 = from c in a1 orderby c.Id select c; //Join连接两个集合相匹配元素,注意:连接条件使用equals关键字!!! var s4 = from c in a1 join t in a2 on c.Id equals t.DogId select new { A1Name = c.Name, DogName = t.DogName }; //Join连接两个集合所有元素 var s5 = from c in a1 join t in a2 on c.Id equals t.DogId select new { A1Name = c, DogName = t }; //分组group...by var s6 = from c in a1 group c by c.Gender; foreach (var group in s6) { Console.WriteLine(group.Key.ToString()); foreach (var item in group) { Console.WriteLine(item.ToString()); } } public class A1 { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public bool Gender { get; set; } //重写ToString方法 public override string ToString() { return Id + "," + Name + "," + Age + "," + Gender; } } public class DogToy { public int DogId { get; set; } public string DogName { get; set; } //重写ToString方法 public override string ToString() { return DogId + "," + DogName; } }
//Linq查询例子: var count = (from u in db.Users where u.Name == userName && u.Pwd == userPwd select u).ToList();//得到一个集合数量,获取用户输入的用户名与密码是否与数据库匹配 var query = (from u in db.Users orderby u.Pwd ascending select u).ToList();//升序排序 var query = (from u in db.Users orderby u.Pwd descending select u).ToList();//降序排序 var query = (from u in db.Users group u by u.Gender).ToList();//将Gender分组 foreach (var group in query) { Console.WriteLine("小组:"+group.Key);//各小组 foreach (var item in group) { Console.WriteLine(item.ToString());//小组成员 } } MethodInfo[] methods = typeof(string).GetMethods(); //第一种方法,获取所有string方法并去除重复的方法 var result = (from m in methods where m.IsStatic != true select m.Name).Distinct().ToList(); //第二种方法,用group by var result1 = (from m in methods where m.IsStatic != true group m by m.Name into g select new { MethodName = g.Key, Overload = g.Count() }).ToList(); //分页 var list1 = db.Users.ToList(); var first = (from c in list1 select c).Skip(0).Take(3).ToList(); //封装分页方法 static List<T> GetPagedListByIndex<T>(List<T> groupList,int pageIndex) { int pageSize = 3; var first = (from c in groupList select c).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); //return groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); return first; } //Join内连接,没有分类的产品查询不到 var innerjoin = from p in ctx.Products join c in ctx.Categories on p.CategoryID equals c.CategoryID select p.ProductName; //外连接,没有分类的产品也能查询到 var leftjoin = from p in ctx.Products join c in ctx.Categories on p.CategoryID equals c.CategoryID into pro from x in pro.DefaultIfEmpty() select p.ProductName;