2014-02-06 200 views
2

我有一種方法可以通過一些linq查詢從數據庫獲取一些數據。數據按預期顯示,但不以我想要的順序顯示。我需要通過查詢1和2中顯示的TotalQuantity屬性對從數據庫中獲得的產品進行排序。我試圖使用OrdeBy,但我不確定如何在此上下文中添加它。需要這個幫助。Linq查詢,如何訂購

這是我的方法:

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; 
} 
+1

您是否收到了某種錯誤或者是你得到不正確的結果? – Yakimych

+0

我得到不正確的結果。它似乎通過「EntityId」而不是TotalQuantity返回產品。你可以在「query2」中看到變量 – koffe14

回答

1

什麼回報

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

更新: 我只能想到再次加入.ToList()來結束的。

+0

這沒有用。試圖:「返回result.OrderBy(opv => opv.TotalQuantity);」但我得到這個錯誤:「錯誤不能隱式轉換類型'System.Linq.IOrderedEnumerable '到'System.Collections.Generic.IList '。顯式轉換存在(你是否缺少一個轉換?)「 – koffe14

1

刪除QUERY2後的第一個ToList()爲下面提到:

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