0
int ddlshopID = ddlSupervisorShop.SelectedItem == null ? 0 : ddlSupervisorShop.SelectedValue.ToInt32();
DateTime today = DateTime.Now;
List<int> supervisorShopList = shops.Select(a => a.ShopID).ToList<int>();
totalSales = (from ds in db.DailySales
              join p in db.Products on ds.ProductID equals p.ProductID
              where ds.IsActive == true && 
                    p.IsActive == true && 
                    ds.SaleDate.Value.Month == today.Month && 
                    ds.SaleDate.Value.Year == today.Year &&
                    ddlshopID == 0 ? supervisorShopList.Contains(ds.ShopID) : ds.ShopID == ddlshopID
              group ds by new
              {
                  p.ProductCategoryID,
                  p.IsActive,
              }
              into g
              select new TotalPriceDivision
              {
                  TotalPrice = g.Sum(a => a.Quantity * a.PerPrice),
                  DivisionID = g.Key.ProductCategoryID
              }).ToList<TotalPriceDivision>();

在这段代码中,行

ds.SaleDate.Value.Month == today.Month && ds.SaleDate.Value.Year == today.Year 

不影响查询结果。ds.SaleDate是数据库中可为空的日期值(不是日期时间)。是不是因为这个?如果是,我该如何解决这个问题?

4

2 回答 2

1
where ds.IsActive && // don't compare boolean with true
      p.IsActive &&
      ds.SaleDate.HasValue && // add this condition
      ds.SaleDate.Value.Month == today.Month && 
      ds.SaleDate.Value.Year == today.Year &&
      ddlshopID == 0 ? supervisorShopList.Contains(ds.ShopID) : ds.ShopID == ddlshopID
于 2012-11-14T13:34:44.237 回答
0

我发现这有什么问题。

出人意料地、难以置信地和愚蠢地

如果我将我的查询订购为

where ds.IsActive == true
&& p.IsActive == true
&& ds.SaleDate.Value.Month == today.Month
&& ds.SaleDate.Value.Year == today.Year
&& ddlshopID == 0 ? supervisorShopList.Contains(ds.ShopID) : ds.ShopID == ddlshopID

日期条件不影响查询结果并且返回错误。

如果我将其订购为

where ds.IsActive == true
&& p.IsActive == true
&& ddlshopID == 0 ? supervisorShopList.Contains(ds.ShopID) : ds.ShopID == ddlshopID
&& ds.SaleDate.Value.Month == today.Month
&& ds.SaleDate.Value.Year == today.Year

它按我的预期工作

我仍然无法相信这怎么可能。如果您认为一定有其他原因,请向我解释。

于 2012-11-14T16:01:18.283 回答