zoukankan      html  css  js  c++  java
  • Linq 实例

    1.分页

    var 分页 = (from c in ctx.Customers select c).Skip(10).Take(10);

    2.分组

    1)一般分组

     
    //根据顾客的国家分组,查询顾客数大于5的国家名和顾客数
    var 一般分组 = from c in ctx.Customers group c by c.Country into g where g.Count() > 5 orderby g.Count() descending select new { 国家 = g.Key, 顾客数 = g.Count() };

    2)匿名类型分组 (根据国家和城市分组,查询顾客覆盖的国家和城市)

     var 匿名类型分组 = from c in ctx.Customers
    
                         group c by new { c.City, c.Country } into g
    
                         orderby g.Key.Country, g.Key.City
    
                         select new
    
                         {
    
                             国家 = g.Key.Country,
    
                             城市 = g.Key.City
    
                         };
    View Code

    3)按条件分组

    //按照是否超重条件分组,分别查询订单数量
    var 按照条件分组 = from o in ctx.Orders
    
                         group o by new { 条件 = o.Freight > 100 } into g
    
                         select new
    
                         {
    
                             数量 = g.Count(),
    
                             是否超重 = g.Key.条件 ? "" : ""
    
                         };

    3distinct

    //查询顾客覆盖的国家
    var 过滤相同项 = (from c in ctx.Customers orderby c.Country select c.Country).Distinct();

    4 union

    //查询城市是A打头和城市包含A的顾客并按照顾客名字排序
    var 连接并且过滤相同项 = (from c in ctx.Customers where c.City.Contains("A") select c).Union
    
                (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);

    5 concat

    //查询城市是A打头和城市包含A的顾客并按照顾客名字排序,相同的顾客信息不会过滤
    var 连接并且不过滤相同项 = (from c in ctx.Customers where c.City.Contains("A") select c).Concat
    
                (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);

    6 取相交项

    //查询城市是A打头的顾客和城市包含A的顾客的交集,并按照顾客名字排序
    var 取相交项 = (from c in ctx.Customers where c.City.Contains("A") select c).Intersect
    
                (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);

    7 排除相交项

    //查询城市包含A的顾客并从中删除城市以A开头的顾客,并按照顾客名字排序
    var 排除相交项 = (from c in ctx.Customers where c.City.Contains("A") select c).Except
    
                (from c in ctx.Customers where c.ContactName.StartsWith("A") select c).OrderBy(c => c.ContactName);

    8 子查询 

    //查询订单数超过5的顾客信息
    var 子查询 = from c in ctx.Customers
    
                       where
    
                           (from o in ctx.Orders group o by o.CustomerID into o where o.Count() > 5 select o.Key).Contains(c.CustomerID)
    
                       select c;

    9 in操作

    //查询指定城市中的客户
     var in操作 = from c in ctx.Customers
    
                        where new string[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City)
    
                        select c;
  • 相关阅读:
    Life -1b The secrets of long life? Teacher: GABRIELE
    Life -1a How well do you sleep? Teacher: GABRIELE
    Kubernetes(K8s) 学习笔记 updating...
    English trip EM3-PE1B Teacher:Taylor
    V3 -A Hard Bargain Teacher: GABRIELE
    802.11k/v/r WiFi无缝漫游
    V3 -Who Am I? Teacher: GABRIELE
    Dell OMSA(updating...)
    (转)红蓝对抗之Windows内网渗透
    WiFi6(整理网上资料进行汇总查看,相关内容摘取与互联网)
  • 原文地址:https://www.cnblogs.com/hunji-fight/p/3371673.html
Copyright © 2011-2022 走看看