0

我想获取表中的最后一个条目并将它们与另一个表连接以生成不匹配的条目。

我收到以下错误

Column is invalid in the HAVING clause 
because it is not contained in either 
an aggregate function or the GROUP BY clause

运行以下命令时适用于 CFWD 和 Balance

var lastEntries = from s in session.Query<Statement>()
                  group s by s.Customer.Id into grp
                  select grp.OrderByDescending(g => g.Id).First();

 var customers = session.Query<Customer>();

 var balances = from s in lastEntries 
                join c in customers on s.Customer.Id equals c.Id 
                where s.Customer.Balance !=  s.CFwd
                select s ;

 foreach (var line in balances )
 {
     ...
 }

但是,在LiNQPad中进行相同测试会产生预期的结果而不会出现错误

var lastEntries = from s in Statements
                  group s by s.Customer.Id into grp
                  select grp.OrderByDescending(g => g.Id).First();

var customers = from s in  Customers select s;

var balances = from s in lastEntries 
       join c in Customers on s.Customer.Id equals c.Id 
       where c.Balance !=  s.CFwd 
       select s;

 balances .Dump();

更新

以下是NHibernate生成的SQL语句

select statement0_.Id as Id0_, 
statement0_.TransactionDate as Transact2_0_, 
statement0_.RefNo as RefNo0_, 
statement0_.Description as Descript4_0_, 
statement0_.BFwd as BFwd0_, 
statement0_.Amount as Amount0_, 
statement0_.CFwd as CFwd0_, 
statement0_.TransactionTypeId as Transact8_0_, 
statement0_.CustomerId as CustomerId0_ 
from Statement statement0_, Customer customer1_ 
where customer1_.Id=statement0_.CustomerId
group by statement0_.CustomerId 
having customer1_.Balance<>statement0_.AmountCFwd 

而 LINQPad 生成以下内容

SELECT [t5].[test], [t5].[Id], [t5].[TransactionDate], 
[t5].[CustomerId], [t5].[RefNo], [t5].[Description], 
[t5].[BFwd], [t5].[Amount], [t5].[CFwd], 
[t5].[TransactionTypeId]
FROM (
    SELECT [t1].[Id]
    FROM [Statement] AS [t0]
    INNER JOIN [Customer] AS [t1] ON [t1].[Id] = [t0].[CustomerId]
    GROUP BY [t1].[Id]
    ) AS [t2]
OUTER APPLY (
    SELECT TOP (1) 1 AS [test], [t3].[Id], [t3].[TransactionDate], 
     [t3].[CustomerId], [t3].[DocRefNo], [t3].[Description], 
     [t3].[BFwd], [t3].[Amount], [t3].[CFwd], 
     [t3].[TransactionTypeId]
    FROM [Statement] AS [t3]
    INNER JOIN [Customer] AS [t4] ON [t4].[Id] = [t3].[CustomerId]
    WHERE [t2].[Id] = [t4].[Id]
    ORDER BY [t3].[Id] DESC
    ) AS [t5]
INNER JOIN [Customer] AS [t6] ON (
    SELECT [t7].[Id]
    FROM [Customer] AS [t7]
    WHERE [t7].[Id] = [t5].[CustomerId]
    ) = [t6].[Id]
WHERE [t6].[Balance] <> [t5].[CFwd]
ORDER BY [t5].[Id] DESC

我尝试重新排列语句以允许分组,但似乎没有正确。

哪个是可以防止错误的正确语法?

4

2 回答 2

1

事实证明,我在考虑 SQL 方式而不是对象。我需要的唯一声明是

var lastEntries = from s in session.Query<Statement>()
              group s by s.Customer.Id into grp
              select grp.OrderByDescending(g => g.Id).First();

并为 Statement 对象的所有列添加分组,如下所示

var lastEntries = from s in session.Query<Statement>()
              group s by new 
                    {
                       s.Customer
                      , s.Id
                       ...
                    } into grp
              select grp.OrderByDescending(g => g.Id).First();
于 2013-02-04T03:18:40.493 回答
0

我认为这可能是一个简单的错字的问题。

在您在 LinqPad 中工作的示例中,您的 where 子句与 join 进行比较Customers

但是,在您的 LinqToNHibernate 示例中,您的 where 子句与sfromlastEntries而不是join customer 进行比较c

所以也许对于你的 NHibernate 例子,你只需要做

var balances = from s in lastEntries 
            join c in customers on s.Customer.Id equals c.Id 
            where c.Balance !=  s.CFwd
            select s ;
于 2013-02-03T21:14:25.130 回答