2014-01-30 59 views
0

試圖找出爲什麼此方法不會返回我在某些DateTime變量中指定的時間範圍內的項目。我確實得到了一個結果,但它沒有任何意義,並提供了不正確的值。不在指定時間範圍內返回項目的方法

該方法應該從昨天(過去24小時)返回所有暢銷書。但是,該方法似乎會返回自一開始就銷售的所有暢銷產品。在數據庫中有一個名爲「CreatedOnUtc」的列,它提供了一個日期,猜測這可以用作測試日期,但我不知道如何訪問它,因爲它在另一個類中。

public IList<BestsellersReportLine> DailyBestsellersReport() 
     { 
      int recordsToReturn = 5; int orderBy = 1; int groupBy = 1; 
      var yesterDay = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)); 
      var earliest = new DateTime(yesterDay.Year, yesterDay.Month, yesterDay.Day, 0, 0, 0); 
      var latest = earliest.Add(new TimeSpan(1, 0, 0, 0, -1)); 
      var currentDay = DateTime.Now; 
      var dayBefore = DateTime.Now.AddDays(-1); 


       var query1 = from opv in _opvRepository.Table 
         where earliest <= currentDay && latest >= dayBefore 
         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 
         select opv; 


       var query2 = groupBy == 1 ? 
        //group by product variants 
         from opv in query1 
         where earliest <= currentDay && latest >= dayBefore 
         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 
         where earliest <= currentDay && latest >= dayBefore 
         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; 

     } 

有一個類似的方法至極已經規定開始時間和結束時間,我相信它會搜索時間記錄在數據庫中。 Allthough此方法顯示在文本框中的可選startDate/endDate視圖中。不過,我需要在代碼中指定DailyBestsellersReport的時間記錄,因爲這將是在後臺運行的進程。

下面是一個類似的時期有日期時間parameteers:

public virtual IList<BestsellersReportLine> BestSellersReport(DateTime? startTime, 
DateTime? endTime,int recordsToReturn = 5, int orderBy = 1, int groupBy = 1) 
{ 
    some code... 
} 

有什麼想法?

+0

你'where'條款實際上並沒有任何測試字段從您的數據庫。你需要測試數據庫字段,而不是一些'c#'變量! –

+0

但我想從數據庫檢索唯一的事情是從BestSellersReportLine EntityId,TotalAmount和TotalQuantity。日期時間變量用於聲明在什麼時間範圍內獲得銷售的產品。就我而言,這是昨天銷售的所有產品。你是說我必須在數據庫中搜索昨天銷售的產品嗎? – koffe14

+0

當然可以。忘記一個咒語的LINQ。如果你這樣做是SQL,那麼'WHERE'子句是什麼樣的? –

回答

1
where earliest <= currentDay && latest >= dayBefore 

這是無用的條件,它是真的。 如果你想過濾你的數據庫記錄,你需要從數據庫測試一些'時間'字段。

更新
我suposed,你已經在_orderRepository.Table 訂購日期列比你代碼片段可以轉化爲類似波紋管:

var currentTime = DateTime.Now; 
var today = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 0, 0, 0); 
var yesterDay = today.Subtract(new TimeSpan(1, 0, 0, 0)); 

//suppose, that we gather all data for yesterday 
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 yesterDay <= o.OrderDate && today >= o.OrderDate 
       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), 
     } 
     ; 
+0

Okey。我不知道該怎麼做,雖然.. – koffe14

+0

有一種方法幾乎做同樣的事情,除了它顯示在一個視圖中,用戶必須在文本框中選擇一個startdate或enddate。這是一種類似的方法,開始/結束日期似乎在可選擇的時間記錄中搜索數據庫。不過,我需要在代碼中設置時間記錄,因爲這是一個後臺處理,它將是一個每天運行一次以顯示昨天暢銷書的調度任務。 - 公共虛擬IList BestSellersReport(DateTime?startTime, DateTime?endTime,int recordsToReturn = 5,int orderBy = 1,int groupBy = 1) {some code ...} – koffe14

相關問題