C# 使用Linq到SQL的Having Sum

C# 使用Linq到SQL的Having Sum,c#,linq-to-sql,C#,Linq To Sql,我有一个SQL命令,我正试图将其转换为LINQ to SQL命令,但遇到了一些困难 我的SQL命令如下所示: SELECT purchs.iclientid, ifeatureid, AddnlOptionList FROM purchs WHERE AddnlOptionList <> '' GROUP BY purchs.iclientid, ifeatureid, AddnlOptionList HAVING (SUM(noptions) > 0) 然而,我似乎被困在q

我有一个SQL命令,我正试图将其转换为LINQ to SQL命令,但遇到了一些困难

我的SQL命令如下所示:

SELECT purchs.iclientid, ifeatureid, AddnlOptionList FROM purchs
WHERE AddnlOptionList <> ''
GROUP BY purchs.iclientid, ifeatureid, AddnlOptionList
HAVING (SUM(noptions) > 0)
然而,我似乎被困在q组,有以下两个错误:

Cannot use local variable 'q' before it is declared

Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer<decimal?> because it is not a delegate type

不能将
WHERE
HAVING
子句都放在一个
WHERE
中。我对其他语法不太熟悉,因此这里是方法语法:

var results = db.Purchases
    .Where(p => p.AddnlOptionList != "")
    .GroupBy(p => new { p.notions, p.iclientid, p.ifeatureid })
    .Where(g => g.Sum(p => p.notions) > 0)
    .SelectMany(g => g)
编辑:转换为Linq语法

var results = from p in db.Purchases
              where p.AddnlOptionList != ""
              group p by new { p.notions, p.iclientid, p.ifeatureid } into g
              where g => g.Sum(p => p.notions) > 0
              from p in g
              select p;
编辑:我没有阅读sql命令。这意味着只拉组,而不是每个组中的每个项目

// method syntax
db.Purchases
    .Where(p => p.AddnlOptionList != "")
    .GroupBy(p => new { p.notions, p.iclientid, p.ifeatureid })
    .Where(g => g.Sum(p => p.notions) > 0)
    .Select(g => g.Key)

// query syntax
    from p in db.Purchases
    where p.AddnlOptionList != ""
    group p by new { p.notions, p.iclientid, p.ifeatureid } into g
    where g.Sum(p => p.notions) > 0
    select new { g.Key.notions, g.Key.iclientid, g.Key.ifeatureid };

不能将
WHERE
HAVING
子句都放在一个
WHERE
中。我对其他语法不太熟悉,因此这里是方法语法:

var results = db.Purchases
    .Where(p => p.AddnlOptionList != "")
    .GroupBy(p => new { p.notions, p.iclientid, p.ifeatureid })
    .Where(g => g.Sum(p => p.notions) > 0)
    .SelectMany(g => g)
编辑:转换为Linq语法

var results = from p in db.Purchases
              where p.AddnlOptionList != ""
              group p by new { p.notions, p.iclientid, p.ifeatureid } into g
              where g => g.Sum(p => p.notions) > 0
              from p in g
              select p;
编辑:我没有阅读sql命令。这意味着只拉组,而不是每个组中的每个项目

// method syntax
db.Purchases
    .Where(p => p.AddnlOptionList != "")
    .GroupBy(p => new { p.notions, p.iclientid, p.ifeatureid })
    .Where(g => g.Sum(p => p.notions) > 0)
    .Select(g => g.Key)

// query syntax
    from p in db.Purchases
    where p.AddnlOptionList != ""
    group p by new { p.notions, p.iclientid, p.ifeatureid } into g
    where g.Sum(p => p.notions) > 0
    select new { g.Key.notions, g.Key.iclientid, g.Key.ifeatureid };

为什么在查询中使用
q
?你不应该通过…进行
分组购物吗?@juharr-这就是我从下面的示例中得到的。当我将其更改为“group purchs by”时,我的where子句停止工作-不包含定义…该示例具有
groupc by…
,它以db.City中的
from c开始。基本上,你必须对你从某个来源得到的东西进行分组。现在,您正在尝试对要将结果分配给的局部变量进行分组。您的where子句部分需要在分组之前进行。为什么在查询中使用
q
?你不应该通过…
进行
分组购物吗?@juharr-这就是我从下面的示例中得到的。当我将其更改为“group purchs by”时,我的where子句停止工作-不包含定义…该示例具有
groupc by…
,它以db.City中的
from c开始。基本上,你必须对你从某个来源得到的东西进行分组。现在,您正在尝试对要将结果分配给的局部变量进行分组。您的where子句部分需要在分组之前进行。已为您进行转换;)@Aron你确定要做一个
SelectMany
?这让我得到了结果,但我得到了原始语句的两倍。我正在试图找出是什么原因造成的。为您进行了转换;)@Aron你确定要做一个
SelectMany
?这让我得到了结果,但我得到了原始语句的两倍。我现在想弄清楚是什么原因造成的。