Question

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
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top