asp.net-mvc - Linq查询,如何orderby

标签 asp.net-mvc linq entity-framework nopcommerce

我有一个方法可以通过一些 linq 查询从数据库中获取一些数据。数据按预期显示,但不是按我想要的顺序显示。我需要按查询 1 和 2 中显示的 TotalQuantity 属性对从数据库获取的产品进行排序。我尝试使用 OrdeBy 但我不确定如何添加在这种背景下。在这方面需要一些帮助。

这是我的方法:

public IList<BestsellersReportLine> DailyBestsellersReport()
{
    OrderStatus os; 
    PaymentStatus ps; 
    ShippingStatus ss;
    int billingCountryId = 0;
    int recordsToReturn = 10; 
    int orderBy = 1;
    int groupBy = 1;

    var range = new
    {
        startTimeUtc = DateTime.Today.AddDays(-1),
        endTimeUtc = DateTime.Today.AddSeconds(-1),
        CreatedOnUtc = DateTime.Today.AddDays(-1),
    };

   var query1 = from opv in _opvRepository.Table
               join o in _orderRepository.Table on opv.OrderId equals o.Id
               join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
               join p in _productRepository.Table on pv.ProductId equals p.Id
               where (o.CreatedOnUtc >= range.startTimeUtc && o.CreatedOnUtc <= range.endTimeUtc)
               select opv;

   var query2 = groupBy == 1 ?
               //group by product variants
               from opv in query1
               group opv by opv.ProductVariantId into g
               select new
               {
                   EntityId = g.Key,
                   TotalAmount = g.Sum(x => x.PriceExclTax),
                   TotalQuantity = g.Sum(x => x.Quantity),
               }
               :
               //group by products
               from opv in query1
               group opv by opv.ProductVariant.ProductId into g
               select new
               {
                   EntityId = g.Key,
                   TotalAmount = g.Sum(x => x.PriceExclTax),
                   TotalQuantity = g.Sum(x => x.Quantity),
               };

   switch (orderBy)
   {
      case 1:
            {
                 query2 = query2.OrderByDescending(x => x.TotalQuantity);
            }
            break;
     case 2:
           {
                 query2 = query2.OrderByDescending(x => x.TotalAmount);
           }
           break;
     default:
           throw new ArgumentException("Wrong orderBy parameter", "orderBy");
    }

    if (recordsToReturn != 0 && recordsToReturn != int.MaxValue)
         query2 = query2.Take(recordsToReturn);

    var result = query2.ToList().Select(x =>
    {
        var reportLine = new BestsellersReportLine()
        {
             EntityId = x.EntityId,
             TotalAmount = x.TotalAmount,
             TotalQuantity = x.TotalQuantity
        };
        return reportLine;
    }).ToList();

    return result;
}

最佳答案

返回怎么样

result.OrderBy(x => x.totalQuantity).ToList();

更新: 我只能想到再次将.ToList()添加到末尾。

关于asp.net-mvc - Linq查询,如何orderby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21597145/

相关文章:

c# - 使用反射和表达式的自动列映射

jquery - $(document).ready() 和部分 View 加载

c# - 生成的 LINQ 类中的继承?

c# - 尝试使用通用的 <T> 集合

c# - 除了 pivot 之外,还有其他方法可以将行字段转换为列吗

c# - 方法名称 (int, DateTime) 没有支持的 SQL 翻译

entity-framework - 更改实体状态

c# - Entity Framework 创建的主键不会自动递增

asp.net-mvc - Dictionary<string,string> 的下拉列表不适用于所选值

c# - 如何确定 Dbcontexts 的范围(以防止整个应用程序的单例上下文)