0

嗨,
我想将 sql 命令转换为 linq,但收到消息:

< Nullable object must have a value >

我的sql查询是:

 Select sum(PosList.Cantity) AS cant, sum(PosList.Value) as mysum,PosList.price,PosList.Name
 from list Inner join PosList On list.ID = PosList.FactID 
 WHERE     (list.FirID = 1) AND (PosList.Date BETWEEN '2011-02-22' AND '2012-02-22') 
 GROUP BY PosList.Name, PosList.Price ORDER BY Value DESC

我的 linq 是:

    var w = (from item in list join itemPos in PosList   on item.ID equals itemPos.FactID      
  where item.FirID == this.firId && item.Date >= date_start && item.Date <= date_stop   
  group itemPos by itemPos.Name into hh   
  let mysum = hh.Sum(s => s.Value)    
  let cant = hh.Sum(n => n.Cantity)   
  let price = hh.Average(i => i.Price)                                  
  orderby mysum descending  
  select new Agent("", 0, 0, "", hh.Key, "", 0, (double)price, (double)cant, (double)mysum, 0, "", "", "", "", ""));

我尝试了以下 linq,但它不起作用:

 var w = (from item in list
 join itemPos in PosList  on item.ID equals itemPos.FactID   
 where item.FirID == this.firId && item.Date >= date_start && item.Date <= date_stop   
 group itemPos by itemPos.Name into hh   
 let mysum = hh.Sum(s => s.Value)                    
orderby mysum descending  
 select new Agent("", 0, 0, "", hh.Key, "", 0, (double)hh.Average(i =>i.Price), (double)hh.Sum(n =>n.Cantity), (double)mysum, 0, "", "", "", "", ""));   

唯一可以正常工作的 linq 是:

   var w = (from item in list
    join itemPos in PosList
   on item.ID equals itemPos.FactID   
    where item.FirID == this.firId && item.Date >= date_start && item.Date <= date_stop   
    group itemPos by itemPos.Name into hh   
   let mysum = hh.Sum(s => s.Value)                    
   orderby mysum descending  
  select new Agent("", 0, 0, "", hh.Key, "", 0, 0, 0, (double)mysum, 0, "", "", "", "",""));   

但我也需要这两个值(价格和数量)。
谢谢!

4

1 回答 1

1

所以该部分(double)hh.Average(i =>i.Price), (double)hh.Sum(n =>n.Cantity)导致异常。这意味着您必须添加条件才能获取 Price 不为空且 Cantity 不为空的记录。并添加i.Price.Valuen.Cantity.Value

(也许这仅适用于 Price 或 Cantity,但我无法从您的代码中看出这一点)。

于 2012-02-23T13:48:53.497 回答