• 【EFCORE笔记】在远程查询数据


    生成操作

    DefaultIfEmpty、Empty、Range Repeat,Iqueryable 只支持 DefaultIfEmpty 方法。

    var blogs = _context.Blogs.Where(b => b.BlogId > 1).DefaultIfEmpty();
    

      

    var defaultBlog = new Blog { Name = "零度" };
    var blogs = _context.Blogs.Where(b => b.BlogId > 1).DefaultIfEmpty(defaultBlog);
    

      

     

    相等比较

    SequenceEqual

    var blogs1 = _context.Blogs.Where(b => b.BlogId > 5);
    
    var blogs2 = _context.Blogs.Where(b => b.BlogId < 2);
    
    var result = blogs1.SequenceEqual(blogs2);
    

      

      

    var blogs1 = _context.Blogs.Where(b => b.BlogId > 5).AsEnumerable();
    
    var blogs2 = _context.Blogs.Where(b => b.BlogId < 2).AsEnumerable();
    
    var result = blogs1.SequenceEqual(blogs2);
    

      

     

    串联运算

    Concat

    var blogs1 = _context.Blogs.Where(b => b.BlogId > 5); 
    
    var blogs2 = _context.Blogs.Where(b => b.BlogId < 2);
    
    var blogs3 = blogs1.Concat(blogs2);
    

      

    联接运算

    Join 和 GroupJoin

    var query = _context.Blogs.Join(_context.Posts,
            b => b.BlogId,
            p => p.BlogId,
            (b, p) => new { b.BlogId, BlogTitle = p.Title, p.Content }
            );
    

      

    var query = _context.Blogs.GroupJoin(_context.Posts,
            b => b.BlogId,
            p => p.BlogId,
            (b, ps) => new { b.BlogId, Posts=ps }
            );
    

      

    数据分组

    GroupBy 和 ToLookup

    var query = _context.Blogs.Where(b=>b.BlogId>2).GroupBy(b=>b.OwnerId);
    

      

    var query = _context.Blogs.Where(b=>b.BlogId>2).GroupBy(b=>b.Owner);
    

      

    var query1 = _context.Blogs.Where(b=>b.BlogId>2).ToLookup(b =>b.OwnerId); 
    
    var query2 = _context.Blogs.Where(b => b.BlogId > 2).ToLookup(b => b.Owner);
    

      

    投影运算

    Select 与 SelectMany

    var query = _context.Blogs.Select(b =>
     new { BlogName = b.Name + "$", b.Url });
    

      

    var query = _context.Blogs
            .Select(b => 
    new { b.BlogId, b.Name, Posts = _context.Posts.Where(p =>p.BlogId == b.BlogId) })
            .SelectMany(b => b.Posts);
    

      

    图解各种JOIN的区别

    深入理解SQL中的Join机制

    SQL夯实基础各种联接查询

    数据分区

    Skip、SkipWhile、Take TakeWhileSkipLast 和 TakeLast

    var query = _context.Blogs.Where(b=>b.BlogId>2).Skip(2);
    

      

    分页实现:OFFSET-FETCH

    var query = _context.Blogs.Where(b=>b.BlogId>2).Take(2);
    

      

    var query = _context.Blogs.Where(b => b.BlogId > 2).Skip(10).Take(10);

    var query = _context.Blogs.SkipWhile(b => b.BlogId > 2);
    
     var query = _context.Blogs.TakeWhile(b => b.BlogId > 2); 
    
    var query = _context.Blogs.SkipLast(2);
    
    var query = _context.Blogs.TakeLast(2);
    

      

    数据排序

    OrderBy、OrderByDescending、ThenBy、ThenByDescending 和 Reverse

    var query = _context.Blogs.Where(b => b.BlogId > 2).OrderBy(b => b.OwnerId);
    

      

    var query = _context.Blogs
    .Where(b => b.BlogId > 2).OrderBy(b => b.OwnerId).OrderByDescending(b=>b.BlogId);
    
    var query = _context.Blogs
    .Where(b => b.BlogId > 2).OrderBy(b => b.OwnerId).ThenByDescending(b 
    => b.BlogId);
    

      

    var query = _context.Blogs.Where(b => b.BlogId > 2).Reverse();
    

      

    var query = _context.Blogs.Where(b => b.BlogId > 2).AsEnumerable().Reverse();
    

      

    集合运算

    Distinct、Except、Intersect 和 Union

    var blogs = _context.Blogs.Where(b => b.BlogId > 5);
    var query = blogs.Distinct();
    

      

    var blogs1 = _context.Blogs.Where(b => b.BlogId > 5); 
    
    var blogs2 = _context.Blogs.Where(b => b.BlogId > 2);
    
    var query = blogs1.Except(blogs2); 
    
    var query = blogs1.Intersect(blogs2); 
    
    var query = blogs1.Union(blogs2);
    

      

     

     

    限定符运算

    All、Any Contains

    bool query = _context.Blogs.All(b => b.BlogId > 5);
    

      

    bool query = _context.Blogs.Any(b => b.BlogId > 5);
    

      

    var names = new string[] { "A", "B", "C" };
    
    bool query = _context.Blogs.Any(b => names.Contains(b.Name));
    

      

    var blog = new Blog { BlogId = 1 };
    bool query = _context.Blogs.Contains(blog);
    

      

    bool query = _context.Blogs.Where(b => b.BlogId > 2).Contains(blog);
    

      

    var query = _context.Blogs.Where(b => b.Name.Contains("ABC"));

    var query = _context.Blogs.Where(b => b.Name.StartsWith("A"));

    var query = _context.Blogs.Where(b => b.Name.EndsWith("ABC"));
    

      

  • 相关阅读:
    JDK内置工具使用(jps、jstack、jmap、jstat)
    解决mybatis嵌套查询使用PageHelper分页不准确
    MySQL变量的使用
    java lambda求和最值
    解决ROS中PLUGINLIB_DECLARE_CLASS错误
    由crt和key文件生成keystore文件
    转载从java进程里dump出类的class文件的小工具--dumpclass
    oracle数据库创建实例
    广告 Spring Boot整合Jasypt增强应用安全
    PostgreSQL10基础(4)插件安装(转载)
  • 原文地址:https://www.cnblogs.com/lbonet/p/14580037.html
Copyright © 2020-2023  润新知