简体   繁体   English

如何在ASP.NET MVC C#中使用LinQ to SQL对从数据库中选择的项目进行排序

[英]How to sort item selected from database using LinQ to SQL in asp.net mvc c#

I want to sort the price and the quantity of my product that query from database to show in my view. 我想对从数据库查询的产品的价格和数量进行排序,以显示在视图中。 I throw the choice to the parameter string of URL (Products?availability=availabilityLowtoHigh&priceSort=priceLowtoHigh&tab=2). 我将选择扔到URL的参数字符串中(产品?availability = availabilityLowtoHigh&priceSort = priceLowtoHigh&tab = 2)。

This is the linq-sql that I used: 这是我使用的linq-sql:

  public IList<Item> Sort(string availability,string priceSort)
  {
      IEnumerable<Item> ien_item;
      if (availability == "availabilityLowtoHigh" && priceSort == "priceLowtoHigh")
      {
            ien_item = from i in this.DataContext.Items

                           orderby i.Quantity ascending
                           orderby i.Price ascending
                           select i;
      }else{
         ien_item = from i in this.DataContext.Items
                       orderby i.Quantity descending
                       orderby i.Price descending
                        select i;
      }
  }

Detail : if the parameter of query string availability == "availabilityLowtoHigh" and priceSort == "priceLowtoHigh", so the product will show in the page by sorting the products that have a less to more quantity, and cheap price to expensive price. 详细信息:如果查询字符串的参数available ==“ availabilityLowtoHigh”和priceSort ==“ priceLowtoHigh”,那么该产品将通过对数量较少,价格从低到高的产品进行排序来显示在页面中。

Can I used orderby twice in my query? 我可以在查询中两次使用orderby吗?

The correct syntax has the orderby keyword once; 正确的语法只有一次orderby关键字; separate multiple orderings with commas: 用逗号分隔多个顺序:

ien_item = from i in this.DataContext.Items 
           orderby i.Quantity ascending, i.Price ascending 
           select i; 

or 要么

ien_item = from i in this.DataContext.Items 
           orderby i.Quantity descending, i.Price descending 
           select i; 

The use of linq in your case is highly redundant. 在您的情况下使用linq是高度冗余的。 You can simply write: 您可以简单地写:

ien_item = DataContext.Items
.OrderByDescending(c => c.Quantity)
.ThenByDescending(c => c.Price);

As you can see a simple lambda is more than enough, no need to resort to linq (which would eventually translated to lambda anyway). 如您所见,简单的lambda绰绰有余,无需诉诸linq(无论如何最终都会转换为lambda)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ASP.NET MVC将所选项目添加到数据库中 - How to add selected item into database using ASP.NET MVC 我如何在ASP.NET MVC中使用Linq,C#从联接表中访问列 - How do I access columns from joined tables using Linq, C# in asp.net MVC 如何使用 Linq 从 C# ASP.NET MVC 4 中的 IQueryable 中找到一个值? - How to find one value from an IQueryable in C# ASP.NET MVC 4 using Linq? 如何从ASP.NET MVC C#中的SQL Server数据库流式传输mp3文件 - How to stream mp3 file from a SQL Server database in ASP.NET MVC C# 将C#ASP.NET MVC 3 SQL数据库转换为html.dropdownlistfor和AutoPostBack to Browse操作 - C# ASP.NET MVC 3 SQL database to html.dropdownlistfor and AutoPostBack to Browse action on selecting and item 使用c#DropDownList项目的asp.net未选中 - asp.net using c# DropDownList item not selected 在ASP.NET MVC中更改下拉列表的选定项目时,如何从数据库填充文本框 - How to fill a textbox from database when dropdownlist's selected item changed in ASP.NET MVC 如何在 Linq &lt;&gt; ASP.NET MVC &lt;&gt; C# 中使用“IN”语句 - How to utilize “IN” statement with Linq <> ASP.NET MVC <> C# 如何更新下拉列表中的选定项asp.net c# - how to update a selected item in a dropdownlist asp.net c# 如何在Asp.net MVC C#中使用Linq从一个表中选择具有最大计数值的多个表中的记录# - how to select records from multiple table with max count value from one table using Linq in Asp.net MVC C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM