C# 在linq上编写sql查询时遇到问题

C# 在linq上编写sql查询时遇到问题,c#,sql,linq,C#,Sql,Linq,我有一个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 b

我有一个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
注意:您可能会将子查询更改为on
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 };

你能写下你有什么问题吗?请更具体一点。X.DocID=Y.DocID不应该是
X.DocID==Y.DocID
(强调
=
)?这正是我需要的!你说过可以用join来写。你能给我看看吗。实际上我确实试过加入,但失败了。非常感谢。
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 };