如何将此Sql语句翻译为Linq

时间:2015-04-10 21:10:31

标签: linq linq-to-entities

如何使用linq编写此查询?

select oa.*
from ord_account_error oe
join
  (select max(ord_account_error_id) [ord_account_error_id]
  from ord_account_error
  group by ord_account_id) e on oe.ord_account_error_id =   e.ord_account_error_id
 join ord_account oa on oe.ord_account_id = oa.ord_account_id
 where oe.error like '%Account in wrong status%'

查询的作用是返回ord_account中的所有行,其中ord_account_error中的最新相关记录包含"帐户处于错误状态"。 我实际上并不需要来自ord_account_error的数据。它只是用于从ord_account中选择正确的行,因此它返回我需要使用的实体。

1 个答案:

答案 0 :(得分:1)

我最好的猜测:

 var qry = from oe in ord_account_error
            .Where(a=>a.error.Contains("Account in wrong status")) 
        join e in ord_account_error
                    .GroupBy(b=>b.ord_account_error_id)
                    .Select(grp=>grp.Max(c=>c.ord_account_error_id))
            on oe.ord_account_error_id equals  e.ord_account_error_id
        join oa in ord_account on oe.ord_account_id equals oa.ord_account_id
        select oa;
相关问题