zoukankan      html  css  js  c++  java
  • LinQ 查询

    查询公司名称及地址

    var 构建匿名类型=from c in ctx.Customers
                               select new
                              {
                                    公司名=c.CompanyName,
                       地址=c.Address
                              };                    

    查询订单号和订单是否超重

    var select 带条件=from o in ctx.Orders
                  select new
                  {
                      订单号=o.OrderID,
                      是否超重=o.Freight>100?"":""
                  };

    查询雇员雇佣年份和名,按照年倒序,按照名正序

    var 排序=from emp in ctx.Employees
      where emp.Employees.Count==0
      orderby emp.HireDate.Value.Year descending,emp.Firstname ascending
      select new
        {
          雇佣年=emp.HireDate.Value.Year,
          名=emp.Firstname
        };

    分页(每页十条记录,查询第二页顾客)

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

    查询顾客覆盖国家

    var 过滤相同项=(from c in ctx.Customers orderby c.Country select c.Country).Distinct():

    查询A开头城市包含A的顾客按照顾客名字排序

    var 连接并且过滤相同项=(from c in ctx.Customers where c.City.Contains("A") select c).Union
                (from c in ctx.Customers where c.ContactName.StartWith("A") select c).OrderBy(c=>c.ContactName);

    同上,相同顾客信息不过滤

    var 连接并且不过滤相同项=(from c in ctx.Customers where c.City.Contains("A") select c).Concat
                (from c in ctx.Customers where c.ContactName.StartWith("A") select c).OrderBy(c=>c.ContactName);

    查询A开头城市包含A的顾客的交集,按照顾客名字排序

    var 连接并且不过滤相同项=(from c in ctx.Customers where c.City.Contains("A") select c).Intersect
                (from c in ctx.Customers where c.ContactName.StartWith("A") select c).OrderBy(c=>c.ContactName);

    查询包含A的顾客并从中删除A开头城市的顾客,按照顾客名字排序  

    var 连接并且不过滤相同项=(from c in ctx.Customers where c.City.Contains("A") select c).Except
                (from c in ctx.Customers where c.ContactName.StartWith("A") select c).OrderBy(c=>c.ContactName);

    子查询

    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;

    内链接,没有分类的商品查询不到

    var innerjoin=from p in ctx.Products
            join c in ctx.Categories
            on p.CategoryID equals c.CategoryID
            select p.ProductName;

    外连接,没有分类的商品也能查询到

    var lefrjoin=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;
  • 相关阅读:
    [Linux/wine.笔记]关于WINE(Linux上运行Windows程序的兼容层)
    [docker.笔记]常用命令
    [技巧.DotNet]超级好用的动态对象ExpandoObject
    .net core 的窗体设计器进展(.NET Core Windows Forms designer),5月中旬或将发布成熟版!
    [问题记录.Oracle/odp.net]托管ODP中,连接池的连接验证参数(validate connection=true)无效?
    [JWT]Json Web Token 备忘
    [MQ]RabbitMQ的概要介绍及消息路由规则
    常见排序算法
    C语言数值存储溢出探讨
    从计算理解数组
  • 原文地址:https://www.cnblogs.com/danznb/p/3569941.html
Copyright © 2011-2022 走看看