How to make conversion SQL Inner joins query vs Entity Framework

a 夏天 提交于 2019-12-21 21:41:34

问题


Here are three tables Service_Orders, Project_Services and Company. There is inner join between 3 tables by Service Order and CompanyID. I want below query to convert into Entity framework with Lambda Express using C# or Vb.net.

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:


Lambda syntax:

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);

Much more readable query syntax:

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);


来源:https://stackoverflow.com/questions/21986818/how-to-make-conversion-sql-inner-joins-query-vs-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!