C# 方法在指定的时间跨度内不返回项
试图找出为什么此方法没有在我用一些C# 方法在指定的时间跨度内不返回项,c#,asp.net-mvc,linq,interface,nopcommerce,C#,Asp.net Mvc,Linq,Interface,Nopcommerce,试图找出为什么此方法没有在我用一些DateTime变量指定的时间跨度内返回项目。我确实得到了一个结果,但它没有意义,并且提供了不正确的值 该方法应该返回昨天(过去24小时)的所有畅销书。然而,该方法似乎返回了从一开始就售出的所有畅销书。在数据库中有一个名为“CreatedOnUtc”的列,其中提供了一个日期,我想这可以用作测试的日期,但我不知道如何访问它,因为它在另一个类中 public IList<BestsellersReportLine> DailyBestsellersR
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;
}
public IList DailyBestsellersReport()
{
int-recordsToReturn=5;int-orderBy=1;int-groupBy=1;
var昨天=DateTime.Now.Subtract(新的时间跨度(1,0,0,0));
var earliest=新日期时间(昨天.年,昨天.月,昨天.日,0,0);
var latest=最早的.Add(新的时间跨度(1,0,0,0,-1));
var currentDay=DateTime.Now;
var dayBefore=DateTime.Now.AddDays(-1);
var query1=来自_opvRepository.Table中的opv
最早=前一天
在opv.OrderId等于o.Id的_orderRepository.Table中加入o
在opv.ProductVariantId上的_productVariantepository.Table中加入pv等于pv.Id
在pv.ProductId等于p.Id的_productRepository.Table中加入p
选择opv;
变量query2=groupBy==1?
//按产品变体分组
来自查询1中的opv
最早=前一天
按opv.productVariatid将opv分组为g
选择新的
{
EntityId=g.键,
总金额=总金额(x=>x.不含税),
总数量=总数量(x=>x数量),
}
:
//按产品分组
来自查询1中的opv
最早=前一天
按opv.ProductVariant.ProductId将opv分组为g
选择新的
{
EntityId=g.键,
总金额=总金额(x=>x.不含税),
总数量=总数量(x=>x数量),
}
;
交换机(订购方)
{
案例1:
{
query2=query2.OrderByDescending(x=>x.TotalQuantity);
}
打破
案例2:
{
query2=query2.OrderByDescending(x=>x.TotalAmount);
}
打破
违约:
抛出新ArgumentException(“错误的orderBy参数”、“orderBy”);
}
if(recordsToReturn!=0&&recordsToReturn!=int.MaxValue)
query2=query2.Take(recordsToReturn);
var result=query2.ToList().Select(x=>
{
var reportLine=新的BestsellersReportLine()
{
EntityId=x.EntityId,
TotalAmount=x.TotalAmount,
TotalQuantity=x.TotalQuantity
};
返回报告行;
}).ToList();
返回结果;
}
有一种类似的方法,它指定了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...
}
公共虚拟IList BestSellersReport(DateTime?startTime,
DateTime?endTime,int-recordsToReturn=5,int-orderBy=1,int-groupBy=1)
{
一些代码。。。
}
有什么想法吗?最早的位置=前一天
where earliest <= currentDay && latest >= dayBefore
这是无用的条件,总是正确的。
如果你想过滤数据库记录,你需要测试数据库中的一些“时间”字段
更新我发现,\ u 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),
}
;
var currentTime=DateTime.Now;
var today=新日期时间(currentTime.Year,currentTime.Month,currentTime.Day,0,0);
var昨天=今天。减去(新的时间跨度(1,0,0,0));
//假设我们收集了昨天的所有数据
var query1=来自_opvRepository.Table中的opv
在opv.OrderId等于o.Id的_orderRepository.Table中加入o
在opv.ProductVariantId上的_productVariantepository.Table中加入pv等于pv.Id
在pv.ProductId等于p.Id的_productRepository.Table中加入p
其中昨天=订单日期
选择opv;
变量query2=groupBy==1?
//按产品变体分组
来自查询1中的opv
按opv.productVariatid将opv分组为g
选择新的
{
EntityId=g.键,
总金额=总金额(x=>x.不含税),
总数量=总数量(x=>x数量),
}
:
//按产品分组
来自查询1中的opv
按opv.ProductVariant.ProductId将opv分组为g
选择新的
{
EntityId=g.键,
总金额=总金额(x=>x.不含税),
总数量=总数量(x=>x数量),
}
;
您的where
子句实际上没有测试数据库中的任何字段。哟