如何进行转换SQL内部联接查询与实体框架

时间:2014-02-24 11:46:13

标签: c# entity-framework entity-framework-4

这里有三个表Service_Orders,Project_Services和Company。 Service Order和CompanyID在3个表之间存在内部联接。 我希望下面的查询使用C#或Vb.net转换为使用Lambda Express的实体框架。

select top 10 * from [Service_Orders] a,[Project_Services] b,[Company] c 
where a.so_no = b.service_order and c.companyId = b.compid

1 个答案:

答案 0 :(得分:14)

Lambda语法:

var query = db.Service_Orders
              .Join(db.Project_Services,
                    a => a.so_no equals,
                    b => b.service_order,
                    (a,b) => new { a, b })
              .Join(db.Company,
                    x => x.b.compid,
                    c => c.companyId,
                    (x,c) => new { x.a, x.b, c })
              .Take(10);

更具可读性的查询语法:

var query = (from a in db.Service_Orders
             join b in db.Project_Services on a.so_no equals b.service_order
             join c in db.Company on b.compid equals c.companyId
             select new { a, b, c }).Take(10);