Linq查询,如何命令

时间:2014-02-06 08:00:27

标签: 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;
}

2 个答案:

答案 0 :(得分:1)

返回怎么样

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

更新: 我只能想到将.ToList()添加到最后。

答案 1 :(得分:1)

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

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