专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-963-0016      微信咨询  |  联系我们

如何使用 Skip 和 Take 方法通过使用查询表达式语法来查询-分区

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:50:26       共计:3672 浏览

本主题中的示例演示如何使用 Skip 和 Take 方法通过使用查询表达式语法来查询 AdventureWorks 销售模型 。 这些示例中使用的 AdventureWorks 销售模型从 AdventureWorks 示例数据库中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 等表生成。


本主题中的示例使用以下 using / Imports 语句:

C#


using System;

using System.Data;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.Objects;

using System.Globalization;

using System.Data.EntityClient;

using System.Data.SqlClient;

using System.Data.Common;


跳过

示例


以下示例使用 Skip 方法以获取 Contact 表中除前五个联系人之外的所有联系人。

C#


using (AdventureWorksEntities context = new AdventureWorksEntities())

{

   // LINQ to Entities only supports Skip on ordered collections.

   IOrderedQueryable<Product> products = context.Products

           .OrderBy(p => p.ListPrice);


   IQueryable<Product> 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 方法以获取 Seattle 的前两个地址之外的所有地址。

C#


using (AdventureWorksEntities context = new AdventureWorksEntities())

{

   ObjectSet<Address> addresses = context.Addresses;

   ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;


   //LINQ to Entities only supports Skip on ordered collections.

   var query = (

       from address in addresses

       from order in orders

       where address.AddressID == order.Address.AddressID

            && address.City == "Seattle"

       orderby order.SalesOrderID

       select 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

示例


以下示例使用 Take 方法以只从 Contact 表中获取前五个联系人。

C#


using (AdventureWorksEntities context = new AdventureWorksEntities())

{

   IQueryable<Contact> first5Contacts = context.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 方法以获取 Seattle 的前三个地址。

C#


String city = "Seattle";

using (AdventureWorksEntities context = new AdventureWorksEntities())

{

   ObjectSet<Address> addresses = context.Addresses;

   ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;


   var query = (

       from address in addresses

       from order in orders

       where address.AddressID == order.Address.AddressID

            && address.City == city

       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);

   }

}


请参阅


   LINQ to Entities 中的查询


版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:如何使用 ToArray ToDictionary 和 ToList 方法-转换 | ·下一条:如何使用 Aggregate 、 Average、Count 、 LongCount Max Min 和 Sum 方法-聚合运算符

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有