使用 Visual Studio 2010 在 WPF 中将 Sql 转为 linq

发布于 2025-01-08 21:59:18 字数 2091 浏览 5 评论 0原文

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

<可空对象必须有一个值 >

我的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, "", "", "", "",""));   

但我也需要这两个值(价格和cantity)..
谢谢 !

Hy,
I want to convert a sql command into linq, but I receive the message :

< Nullable object must have a value >

My sql query is:

 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

My linq is:

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

I tried the following linq, but it did not work:

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

The only linq which works ok is:

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

but I need those two values(price and cantity) also..
Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2025-01-15 21:59:18

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

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

So the part (double)hh.Average(i =>i.Price), (double)hh.Sum(n =>n.Cantity) causes the exception. Which means that you must add conditions to get the records where Price is not null and Cantity is not null. And add i.Price.Value and n.Cantity.Value.

(Maybe this applies to Price or Cantity only, but I cannot tell that from your code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文