遇到特定Linq-To-Entity表达式的麻烦

时间:2012-10-13 15:25:16

标签: c# entity-framework

我还在学习Entity表达式的绳索,我想将这个SQL查询实现到Entity表达式中,查询是:

SELECT classname,abbreviation, cid
            FROM [db].[dbo].[classes] AS c
            WHERE c.cid IN (
                SELECT DISTINCT(CID)
                FROM [db].[dbo].[CDS]
                WHERE 
                        sid = '1'
                        AND
                        did = '24'
                    )
            ORDER BY abbreviation, classname 

我如何将该查询实现到实体表达式?

1 个答案:

答案 0 :(得分:0)

表达式为:

context.Classes.Where(C => 
                (context.CDS.Where(CD => CD.sid == "1" && CD.did == "24")
                .Distinct(CD => CD.CID)).Contains(C.CID))
                .Select(C => new { 
                                    className = C.className, 
                                    abbreviation = C.abbreviation, 
                                    cid = C.cid 
                                 });

希望这会有所帮助!!