• LINQ学习笔记(三)之LINQ to SQL 查询语句使用方法一


          一、使用Select:

          1、描述:查询顾客表中的公司名、地址信息。

          查询语句如下:

          var p =

          var p = from c in ctx.Customers

                          select new

                          {

                              公司名 = c.CompanyName,

                              地址 = c.Address

                          };  

          2、描述:查询职员表中的姓名和雇用年份

          查询语句如下:      

          var p = from emp in ctx.Employees

                          select new

                          {

                              姓名 = emp.LastName + emp.FirstName,

                             雇用年 = emp.HireDate.Value.Year

                          }; 

          3、描述:查询顾客的ID以及联系信息(职位和联系人)

          查询语句如下: 

          var p = from c in ctx.Customers

                          select new

                          {

                              ID = c.CustomerID,

                              联系信息 = new

                              {

                                  职位 = c.ContactTitle,

                                  联系人 = c.ContactName

                              }

                          };

          4、描述:查询订单号和订单是否超重的信息

          查询语句如下:

          var p = from o in ctx.Orders

                            select new

                            {

                                订单号 = o.OrderID,

                                是否超重 = o.Freight > 100 ? "" : ""

                            };

          二、使用Where:

          1、描述:查询顾客的国家、城市和订单数信息,要求国家是法国并且订单数大于5

          查询语句如下:    

          var p = from c in ctx.Customers

                      where c.Country == "France" && c.Orders.Count > 5

                      select new

                      {

                          国家 = c.Country,

                          城市 = c.City,

                          订单数 = c.Orders.Count

                      };    

          2、描述:查询顾客的所有信息,要求城市是伦敦的客户

          查询语句如下:

          var q =
              from c in db.Customers
              where c.City == "London"
             
    select c;

          3、筛选1994 年或之后雇用的雇员

          查询语句如下:  

          var q =
              from e in db.Employees
              where e.HireDate >= new DateTime(1994, 1, 1)
              select e;

          4、筛选库存量在订货点水平之下但未断货的产品

          查询语句如下:

          var q =
              from p in db.Products
              where p.UnitsInStock <= p.ReorderLevel && !p.Discontinued
              select p;

          5、选择CustomerID 为“BONAP”的单个客户

          查询语句如下:

          Customer cust = db.Customers.First(c => c.CustomerID == "BONAP");

          三、使用Order By

          1、描述:查询所有没有下属雇员的雇用年和名,按照雇用年倒序,按照名正序

          查询语句如下:      

          var p = 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

                     };    

          2、描述:对客户进行排序,先按City排序,当City相同时,按ContactName排序。

          用Lambda表达式查询语句如下:

          var q = 
              db.Customers
              .OrderByDescending(c => c.City)
              .ThenByDescending(c => c.ContactName).ToList();

          四、使用分页:

          描述:按照每页10条记录,查询第二页的顾客

          查询语句如下:

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

          对应SQL语句如下:

          SELECT TOP 10 [t1].[CustomerID], [t1].[CompanyName], [t1].[ContactName], [t1].[ContactTitle], [t1].[Address], [t1].[City],

                        [t1].[Region], [t1].[PostalCode], [t1].[Country], [t1].[Phone], [t1].[Fax]

          FROM (

                    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle],

                           [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone],

                           [t0].[Fax]) AS [ROW_NUMBER], [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle],

                           [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]

                    FROM [dbo].[Customers] AS [t0]

         ) AS [t1]

         WHERE [t1].[ROW_NUMBER] > @p0

         -- @p0: Input Int32 (Size = 0; Prec = 0; Scale = 0) [10]

     

     

     

     

     

     

      

     

     

     

  • 相关阅读:
    2020/10/29
    2020/10/24
    2020/10/28
    2020/10/31周报
    linux shell 中判断字符串为空的正确方法
    20201107 千锤百炼软工人
    20201103 千锤百炼软工人
    20201109 千锤百炼软工人
    20201111 千锤百炼软工人
    20201105 千锤百炼软工人
  • 原文地址:https://www.cnblogs.com/maocs/p/1497778.html
Copyright © 2020-2023  润新知