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

如何使用 ThenBy 方法通过使用基于方法的查询语法来查询-中间件排序

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

本主题中的示例演示如何使用 ThenBy 方法通过使用基于方法的查询语法来查询 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;


ThenBy

示例


采用基于方法的查询语法的以下示例使用 OrderBy 和 ThenBy 以返回先按姓氏后按名字排序的联系人列表。

C#


using (AdventureWorksEntities context = new AdventureWorksEntities())

{

   IQueryable<Contact> sortedContacts = context.Contacts

       .OrderBy(c => c.LastName)

       .ThenBy(c => c.FirstName);


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

   }

}


ThenByDescending

示例


以下示例使用 OrderBy 和 ThenByDescending 方法以首先按标价排序,然后执行产品名称的降序排序。

C#


using (AdventureWorksEntities context = new AdventureWorksEntities())

{

   IOrderedQueryable<Product> query = context.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);

   }

}


请参阅


   LINQ to Entities 中的查询


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

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

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