Linq问题选择一列减去不同表中另一列的总和

发布于 2024-11-06 07:39:41 字数 276 浏览 1 评论 0原文

这是我的 sql 代码,

SELECT   M.PartNo AS ModulePartNo,
(SELECT SUM(Delivered - Allocated) 
FROM V_InStock Stk
WHERE Stk.PartNo = M.PartNo) AS Available
FROM V_Products M
WHERE M.PartNo='100-25897'

我在使其在 Linq 中工作时遇到了真正的问题,无法计算出总和部分 - 任何帮助将不胜感激

This is my sql code

SELECT   M.PartNo AS ModulePartNo,
(SELECT SUM(Delivered - Allocated) 
FROM V_InStock Stk
WHERE Stk.PartNo = M.PartNo) AS Available
FROM V_Products M
WHERE M.PartNo='100-25897'

I am having real problems getting this to work in Linq, can't work out the sum part - any help would be really appreciated

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

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

发布评论

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

评论(2

薄暮涼年 2024-11-13 07:39:41

这应该有效。

var query = from stk in V_InStock
            group stk by stk.PartNo into stkg
            where stkg.Key == '100-25897'
            select new
            {
              ModulePartNo = stkg.Key,
              Available = stkg.Sum(s => s.Delivered) - stkg.Sum(s => s.Allocated)
            }

This should work.

var query = from stk in V_InStock
            group stk by stk.PartNo into stkg
            where stkg.Key == '100-25897'
            select new
            {
              ModulePartNo = stkg.Key,
              Available = stkg.Sum(s => s.Delivered) - stkg.Sum(s => s.Allocated)
            }
卷耳 2024-11-13 07:39:41

如果您需要进行聚合分组等,您可能需要设置一个对象来“选择”所有内容。然而,对于直接连接来说,这样的事情应该可行。

  var outputObject = (from t1 in dbContext.table1
  join t2 in dbContext.table2 on t1.PartNo equals t2.PartNo
  select (t1.Delivered - t2.Allocated));

If you need to do aggregate grouping etc you will probably need to set up an object to "select" everything into. However for a straight join something like this should work.

  var outputObject = (from t1 in dbContext.table1
  join t2 in dbContext.table2 on t1.PartNo equals t2.PartNo
  select (t1.Delivered - t2.Allocated));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文