博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linq查询
阅读量:5086 次
发布时间:2019-06-13

本文共 3502 字,大约阅读时间需要 11 分钟。

//Linq查询List
a1 = new List
();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
a2 = new List
();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...byvar 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 byvar 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
GetPagedListByIndex
(List
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;

 

转载于:https://www.cnblogs.com/genesis/p/4908995.html

你可能感兴趣的文章