【问题标题】:Linq query, how to orderbyLinq 查询,如何排序
【发布时间】:2014-02-06 08:00:27
【问题描述】:

我有一种方法可以通过一些 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;
}

【问题讨论】:

  • 您是否遇到了某种错误,或者您是否获得了不正确的结果?
  • 我得到的结果不正确。它似乎通过“EntityId”而不是 TotalQuantity 返回产品。您可以在“query2”中看到变量

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


【解决方案1】:

退货呢

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

更新: 我只能想着再把 .ToList() 加到最后。

【讨论】:

  • 那行不通。尝试:“return result.OrderBy(opv => opv.TotalQuantity);”但我收到此错误:“错误 7 无法将类型 'System.Linq.IOrderedEnumerable' 隐式转换为' System.Collections.Generic.IList'。存在显式转换(您是否缺少演员表?)"
【解决方案2】:

删除query2之后的第一个ToList(),如下所述:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多