• Linq查询


    //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;
  • 相关阅读:
    mysql,windows自动备份设置
    彻底搞清楚javascript中的require、import和export
    Spring Boot 打包报错Failed to execute goal org.apache.maven.plugins:mavenresourcesplugin:3.2.0
    Spring AOP 切点切面
    12.5M 30M 90M DEM免费下载!【转】
    JS 中的数组遍历方式效率比较[转]
    cesium加载CAD模型(.dwg)
    Cesium发布下一代3D Tiles规范预览
    cesium点击面高亮事件[转]
    MySQL 5.7及8.0版本数据库的root密码遗忘的解决办法
  • 原文地址:https://www.cnblogs.com/genesis/p/4908995.html
Copyright © 2020-2023  润新知