转:Linq to SQL 经典查询 记录

SELECT
描述:查询顾客的公司名、地址信息

查询句法:

var 构建匿名类型1 = from c in ctx.Customers

                      select new

                      {

                          公司名 = c.CompanyName,

                          地址 = c.Address

                      };

描述:查询职员的姓名和雇用年份

查询句法:

var 构建匿名类型2 = from emp in ctx.Employees

                      select new

                      {

                          姓名 = emp.LastName + emp.FirstName,

                          雇用年 = emp.HireDate.Value.Year

                      };

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

查询句法:

var 构建匿名类型3 = from c in ctx.Customers

                      select new

                      {

                          ID = c.CustomerID,

                          联系信息 = new

                          {

                              职位 = c.ContactTitle,

                              联系人 = c.ContactName

                          }

                      };



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

查询句法:

var select带条件 = from o in ctx.Orders

                        select new

                        {

                            订单号 = o.OrderID,

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

                        };

子查询

描述:查询订单数超过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;


in 操作

描述:查询指定城市中的客户

查询句法:

        var in操作 = from c in ctx.Customers

                    where new string[] { "Brandenburg", "Cowes", "Stavern" }.Contains(c.City)

                    select c;

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;

原文地址:https://www.cnblogs.com/dudu837/p/1833958.html