我有一个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
上的join,但是我照原样离开了,因此您可以看到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 };
关于c# - 在linq上编写SQL查询时遇到问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7684083/