在linq上编写sql查询时遇到问题
本文关键字:查询 遇到 问题 sql linq | 更新日期: 2023-09-27 18:10:34
我有一个返回所有帐户的SQL查询,在结果表的顶部,我有共享相同文档的帐户。下面的查询工作得很好,但是在linq上实现相同的逻辑有一个问题。
select
(
select
COUNT(*)
from Signs X, Signs Y
where X.AccountID = 2 and Y.AccountID = A.ID and X.DocID = Y.DocID
),
*
from
Accounts A
where
A.ID != 2
order by 1 desc
var result = from A in Accounts
where A.ID != 2
select new { Count = (from X in Signs
from Y in Signs
where X.AccountID == 2 &&
Y.AccountID == A.ID &&
X.DocID == Y.DocID
select 1).Count(),
A };
注意:您可能会将子查询更改为DocID
上的连接,但我已经离开了,所以您可以看到SQL和LINQ之间的相似性。
连接示例:
var result = from A in Accounts
where A.ID != 2
select new { Count = (from X in Signs
join Y in Signs on X.DocID equals Y.DocID
where X.AccountID == 2 &&
Y.AccountID == A.ID
select 1).Count(),
A };