将带有子查询的sql查询转换为linq语句

时间:2013-01-14 17:09:43

标签: c# sql linq sql-server-2008

  

可能重复:
  how to do subquery in LINQ

我有一个SQL Server 2008查询:

select 
    account_hcc_id, account_name 
from 
    [ACCOUNT_HISTORY_FACT] 
where 
     TOP_ACCOUNT_KEY = (select TOP 1 TOP_ACCOUNT_KEY 
                        from ACCOUNT_HISTORY_FACT 
                        where account_hcc_id = '3362') 
     and ACCOUNT_LEVEL = 1;

我需要将其转换为C#linq语句。请让我知道我该怎么做。

1 个答案:

答案 0 :(得分:4)

from ahf in db.ACCOUNT_HISTORY_FACT
where ahf.ACCOUNT_LEVEL == 1 &&
      ahf.TOP_ACCOUNT_KEY == db.ACCOUNT_HISTORY_FACT
                               .Where(x => x.account_hcc_id == "3362") 
                               .Select(x => x.TOP_ACCOUNT_KEY)
                               .FirstOrDefault() 
select new { ahf.account_hcc_id, ahf.account_name };