方法不在指定的时间范围内返回项目

时间:2014-01-30 08:43:59

标签: c# asp.net-mvc linq interface nopcommerce

试图弄清楚为什么这个方法没有在我用一些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;

        }

有一个类似的方法指定了startTime和endTime,我相信它会在时间记录中搜索数据库。尽管此方法显示在文本框中可选择的startDate / endDate的视图中。但是我需要在代码中指定DailyBestsellersReport的时间记录,因为这将是一个在后台运行的进程。

这是与DateTime参数的类似时期:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

where earliest <= currentDay && latest >= dayBefore

这是无用的条件,它总是如此。 如果你想过滤你的数据库记录,你需要测试数据库中的某些“时间”字段。

更新
我说,你在_orderRepository.Table中有OrderDate列 你的片段可以转化为类似下面的东西:

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),
       }
       ;