实体框架(Entity Framework )是 ADO.NET 中的一套支持开发面向数据的软件应用程序的技术。
LINQ to Entities 提供语言集成查询 (LINQ) 支持,它允许开发人员使用 Visual Basic 或 Visual C# 根据实体框架概念模型编写查询。针对实体框架的查询由针对对象上下文执行的命令目录树查询表示。LINQ to Entities 将语言集成查询 (LINQ) 查询转换为命令目录树查询,针对实体框架执行这些查询,并返回可同时由实体框架和 LINQ 使用的对象。
下面列出一些基于方法的查询语法示例(C#):
投影 Select | SelectMany
筛选 Where | Where…Contains
排序 ThenBy | ThenByDescending
聚合运算符 Average | Count | LongCount | Max | Min | Sum
转换 ToArray | ToDictionary | ToList
元素运算符 First
分组 GroupBy
以下示例使用 Select 方法以将 Product.Name 和 Product.ProductID 属性投影到一系列匿名类型。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { var query = AWEntities.Products .Select(product => new { ProductId = product.ProductID, ProductName = product.Name }); Console.WriteLine("Product Info:"); foreach (var productInfo in query) { Console.WriteLine("Product Id: {0} Product name: {1} ", productInfo.ProductId, productInfo.ProductName); } }
以下示例使用 Select 方法以只返回一系列产品名称。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { IQueryable productNames = AWEntities.Products .Select(p => p.Name); Console.WriteLine("Product Names:"); foreach (String productName in productNames) { Console.WriteLine(productName); } }
以下示例使用 SelectMany 方法以选择 TotalDue 低于 500.00 的所有订单。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet contacts = AWEntities.Contacts; ObjectSet orders = AWEntities.SalesOrderHeaders; var query = contacts.SelectMany( contact => orders.Where(order => (contact.ContactID == order.Contact.ContactID) && order.TotalDue < 500.00M) .Select(order => new { ContactID = contact.ContactID, LastName = contact.LastName, FirstName = contact.FirstName, OrderID = order.SalesOrderID, Total = order.TotalDue })); foreach (var smallOrder in query) { Console.WriteLine("Contact ID:{0} Name:{1},{2} Order ID:{3} Total Due: ${4} ", smallOrder.ContactID, smallOrder.LastName, smallOrder.FirstName, smallOrder.OrderID, smallOrder.Total); } }
以下示例使用 SelectMany 方法以选择在 2002 年 10 月 1 或此日期之后发出的所有订单。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet contacts = AWEntities.Contacts; ObjectSet orders = AWEntities.SalesOrderHeaders; var query = contacts.SelectMany( contact => orders.Where(order => (contact.ContactID == order.Contact.ContactID) && order.OrderDate >= new DateTime(2002, 10, 1)) .Select(order => new { ContactID = contact.ContactID, LastName = contact.LastName, FirstName = contact.FirstName, OrderID = order.SalesOrderID, OrderDate = order.OrderDate })); foreach (var order in query) { Console.WriteLine("Contact ID:{0} Name:{1},{2} Order ID:{3} Order date: {4:d} ", order.ContactID, order.LastName, order.FirstName, order.OrderID, order.OrderDate); } }
以下示例返回所有联机订单。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { var onlineOrders = AWEntities.SalesOrderHeaders .Where(order => order.OnlineOrderFlag == true) .Select(s => new { s.SalesOrderID, s.OrderDate, s.SalesOrderNumber }); foreach (var onlineOrder in onlineOrders) { Console.WriteLine("Order ID: {0} Order date: {1:d} Order number: {2}", onlineOrder.SalesOrderID, onlineOrder.OrderDate, onlineOrder.SalesOrderNumber); } }
以下示例返回订单数量大于 2 且小于 6 的订单。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { var query = AWEntities.SalesOrderDetails .Where(order => order.OrderQty > 2 && order.OrderQty < 6) .Select(s => new { s.SalesOrderID, s.OrderQty }); foreach (var order in query) { Console.WriteLine("Order ID: {0} Order quantity: {1}", order.SalesOrderID, order.OrderQty); } }
以下示例返回所有红色产品。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { var query = AWEntities.Products .Where(product => product.Color == "Red") .Select(p => new { p.Name, p.ProductNumber, p.ListPrice }); foreach (var product in query) { Console.WriteLine("Name: {0}", product.Name); Console.WriteLine("Product number: {0}", product.ProductNumber); Console.WriteLine("List price: ${0}", product.ListPrice); Console.WriteLine(""); } }
以下示例将一个数组用作 Where¡Contains 子句的一部分,以查找 ProductModelID 与数组中的值匹配的所有产品。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { int?[] productModelIds = { 19, 26, 118 }; var products = AWEntities.Products. Where(p => productModelIds.Contains(p.ProductModelID)); foreach (var product in products) { Console.WriteLine("{0}: {1}", product.ProductModelID, product.ProductID); } }
作为 Where¡Contains 子句中谓词的一部分,您可以使用 Array、List<(Of <(<'T>)>)> 或实现IEnumerable<(Of <(<'T>)>)> 接口的任何类型的集合。 还可以在 LINQ to Entities 查询中声明和初始化集合。
以下示例声明并初始化 Where¡Contains 子句中的数组,以查找 ProductModelID 或 Size 与数组中的值匹配的所有产品。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { var products = AWEntities.Products. Where(p => (new int?[] { 19, 26, 18 }).Contains(p.ProductModelID) || (new string[] { "L", "XL" }).Contains(p.Size)); foreach (var product in products) { Console.WriteLine("{0}: {1}, {2}", product.ProductID, product.ProductModelID, product.Size); } }
采用基于方法的查询语法的以下示例使用 OrderBy 和 ThenBy 以返回先按姓氏后按名字排序的联系人列表。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { IQueryable sortedContacts = AWEntities.Contacts .OrderBy(c => c.LastName) .ThenBy(c => c.FirstName) .Select(c => c); Console.WriteLine("The list of contacts sorted by last name then by first name:"); foreach (Contact sortedContact in sortedContacts) { Console.WriteLine(sortedContact.LastName + ", " + sortedContact.FirstName); } }
以下示例使用 OrderBy 和 ThenByDescending 方法以首先按标价排序,然后执行产品名称的降序排序。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet products = AWEntities.Products; IOrderedQueryable query = products .OrderBy(product => product.ListPrice) .ThenByDescending(product => product.Name); foreach (Product product in query) { Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}", product.ProductID, product.Name, product.ListPrice); } }
以下示例使用 Average 方法来查找产品的平均标价。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet products = AWEntities.Products; Decimal averageListPrice = products.Average(product => product.ListPrice); Console.WriteLine("The average list price of all the products is ${0}", averageListPrice); }
以下示例使用 Average 方法以查找每种样式的产品的平均标价。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet products = AWEntities.Products; var query = from product in products group product by product.Style into g select new { Style = g.Key, AverageListPrice = g.Average(product => product.ListPrice) }; foreach (var product in query) { Console.WriteLine("Product style: {0} Average list price: {1}", product.Style, product.AverageListPrice); } }
以下示例使用 Average 方法以查找平均应付款总计。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; Decimal averageTotalDue = orders.Average(order => order.TotalDue); Console.WriteLine("The average TotalDue is {0}.", averageTotalDue); }
以下示例使用 Average 方法以获取每个联系人 ID 的平均应付款总计。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; var query = from order in orders group order by order.Contact.ContactID into g select new { Category = g.Key, averageTotalDue = g.Average(order => order.TotalDue) }; foreach (var order in query) { Console.WriteLine("ContactID = {0} \t Average TotalDue = {1}", order.Category, order.averageTotalDue); } }
以下示例使用 Average 方法以针对每个联系人获取具有平均应付款总计的订单。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; var query = from order in orders group order by order.Contact.ContactID into g let averageTotalDue = g.Average(order => order.TotalDue) select new { Category = g.Key, CheapestProducts = g.Where(order => order.TotalDue == averageTotalDue) }; foreach (var orderGroup in query) { Console.WriteLine("ContactID: {0}", orderGroup.Category); foreach (var order in orderGroup.CheapestProducts) { Console.WriteLine("Average total due for SalesOrderID {1} is: {0}", order.TotalDue, order.SalesOrderID); } Console.Write("\n"); } }
以下示例使用 Count 方法以返回 Product 表中的产品数量。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet products = AWEntities.Products; int numProducts = products.Count(); Console.WriteLine("There are {0} products.", numProducts); }
以下示例使用 Count 方法以返回联系人 ID 的列表和每个联系人 ID 所具有的订单数。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet contacts = AWEntities.Contacts; //Can't find field SalesOrderContact var query = from contact in contacts select new { CustomerID = contact.ContactID, OrderCount = contact.SalesOrderHeaders.Count() }; foreach (var contact in query) { Console.WriteLine("CustomerID = {0} \t OrderCount = {1}", contact.CustomerID, contact.OrderCount); } }
以下示例按颜色对产品进行分组,并使用 Count 方法以返回每个颜色组中的产品数量。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet products = AWEntities.Products; var query = from product in products group product by product.Color into g select new { Color = g.Key, ProductCount = g.Count() }; foreach (var product in query) { Console.WriteLine("Color = {0} \t ProductCount = {1}", product.Color, product.ProductCount); } }
以下示例以长整型获取联系人计数。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet contacts = AWEntities.Contacts; long numberOfContacts = contacts.LongCount(); Console.WriteLine("There are {0} Contacts", numberOfContacts); }
以下示例使用 Max 方法以获取最大应付款总计。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; Decimal maxTotalDue = orders.Max(w => w.TotalDue); Console.WriteLine("The maximum TotalDue is {0}.", maxTotalDue); }
以下示例使用 Max 方法以获取每个联系人 ID 的最大应付款总计。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; var query = from order in orders group order by order.Contact.ContactID into g select new { Category = g.Key, maxTotalDue = g.Max(order => order.TotalDue) }; foreach (var order in query) { Console.WriteLine("ContactID = {0} \t Maximum TotalDue = {1}", order.Category, order.maxTotalDue); } }
以下示例使用 Max 方法以针对每个联系人 ID 获取具有最大应付款总计的订单。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; var query = from order in orders group order by order.Contact.ContactID into g let maxTotalDue = g.Max(order => order.TotalDue) select new { Category = g.Key, CheapestProducts = g.Where(order => order.TotalDue == maxTotalDue) }; foreach (var orderGroup in query) { Console.WriteLine("ContactID: {0}", orderGroup.Category); foreach (var order in orderGroup.CheapestProducts) { Console.WriteLine("MaxTotalDue {0} for SalesOrderID {1}: ", order.TotalDue, order.SalesOrderID); } Console.Write("\n"); } }
以下示例使用 Min 方法以获取最小应付款总计。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; Decimal smallestTotalDue = orders.Min(totalDue => totalDue.TotalDue); Console.WriteLine("The smallest TotalDue is {0}.", smallestTotalDue); }
以下示例使用 Min 方法以获取每个联系人 ID 的最小应付款总计。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; var query = from order in orders group order by order.Contact.ContactID into g select new { Category = g.Key, smallestTotalDue = g.Min(order => order.TotalDue) }; foreach (var order in query) { Console.WriteLine("ContactID = {0} \t Minimum TotalDue = {1}", order.Category, order.smallestTotalDue); } }
以下示例使用 Min 方法以针对每个联系人获取具有最小应付款总计的订单。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; var query = from order in orders group order by order.Contact.ContactID into g let minTotalDue = g.Min(order => order.TotalDue) select new { Category = g.Key, smallestTotalDue = g.Where(order => order.TotalDue == minTotalDue) }; foreach (var orderGroup in query) { Console.WriteLine("ContactID: {0}", orderGroup.Category); foreach (var order in orderGroup.smallestTotalDue) { Console.WriteLine("Mininum TotalDue {0} for SalesOrderID {1}: ", order.TotalDue, order.SalesOrderID); } Console.Write("\n"); } }
以下示例使用 Sum 方法以获取 SalesOrderDetail 表中订单数量的总数。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderDetails; double totalOrderQty = orders.Sum(o => o.OrderQty); Console.WriteLine("There are a total of {0} OrderQty.", totalOrderQty); }
以下示例使用 Sum 方法以获取每个联系人 ID 的应付款总计。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet orders = AWEntities.SalesOrderHeaders; var query = from order in orders group order by order.Contact.ContactID into g select new { Category = g.Key, TotalDue = g.Sum(order => order.TotalDue) }; foreach (var order in query) { Console.WriteLine("ContactID = {0} \t TotalDue sum = {1}", order.Category, order.TotalDue); } }
以下示例使用 Skip<(Of <<'(TSource>)>>) 方法以获取 Contact 表中除前三个联系人之外的所有联系人。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { // LINQ to Entities only supports Skip on ordered collections. IOrderedQueryable products = AWEntities.Products .OrderBy(p => p.ListPrice); IQueryable allButFirst3Products = products.Skip(3); Console.WriteLine("All but first 3 products:"); foreach (Product product in allButFirst3Products) { Console.WriteLine("Name: {0} \t ID: {1}", product.Name, product.ProductID); } }
以下示例使用 Skip<(Of <<'(TSource>)>>) 方法以获取 Seattle 的前两个地址之外的所有地址。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSetaddresses = AWEntities.Addresses;ObjectSet orders = AWEntities.SalesOrderHeaders;//LINQ to Entities only supports Skip on ordered collections.var query = (from address in addressesfrom order in orderswhere address.AddressID == order.Address.AddressID&& address.City == "Seattle"orderby order.SalesOrderIDselect new {City = address.City, OrderID = order.SalesOrderID, OrderDate = order.OrderDate}).Skip(2);Console.WriteLine("All but first 2 orders in Seattle:");foreach (var order in query){Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",order.City, order.OrderID, order.OrderDate);} }
以下示例使用 Take<(Of <<'(TSource>)>>) 方法以只从 Contact 表中获取前五个联系人。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet contacts = AWEntities.Contacts; IQueryable first5Contacts = contacts.Take(5); Console.WriteLine("First 5 contacts:"); foreach (Contact contact in first5Contacts) { Console.WriteLine("Title = {0} \t FirstName = {1} \t Lastname = {2}", contact.Title, contact.FirstName, contact.LastName); } }
以下示例使用 Take<(Of <<'(TSource>)>>) 方法以获取 Seattle 的前三个地址。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSetaddresses = AWEntities.Addresses;ObjectSet orders = AWEntities.SalesOrderHeaders;var query = (from address in addressesfrom order in orderswhere address.AddressID == order.Address.AddressID&& address.City == "Seattle"select new {City = address.City, OrderID = order.SalesOrderID, OrderDate = order.OrderDate}).Take(3);Console.WriteLine("First 3 orders in Seattle:");foreach (var order in query){Console.WriteLine("City: {0} Order ID: {1} Total Due: {2:d}",order.City, order.OrderID, order.OrderDate);} }
下面的示例使用 ToArray<(Of <<'(TSource>)>>) 方法立即将序列计算为数组。
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities()) { ObjectSet products = AWEntities.Products; Product[] prodArray = ( from product in products orderby product.ListPrice descending select product).ToArray(); Console.WriteLine("Every price from highest to lowest:"); foreach (Product product in prodArray) { Console.WriteLine(product.ListPrice); } }