简体   繁体   中英

Is it possible to get result from joined SQL query from several tables in C# with Entity Framework?

I am using.Net Core with C# and Entity Framework. I need to get data for reporting and it requires joins with several tables.

My question is, what is the best way to call the SQL query and pass the result to a client application?

If the number of tables used is less than 3 or 4, we can use the EF join to get the data from multiple tables.

Sample linq Join

var q=(from pd in dataContext.tblProducts 
 join od in dataContext.tblOrders on pd.ProductID equals od.ProductID 
 join ct in dataContext.tblCustomers 
 on new {a=od.CustomerID,b=od.ContactNo} equals new {a=ct.CustID,b=ct.ContactNo} 
 orderby od.OrderID 
 select new { 
 od.OrderID,
 pd.ProductID,
 pd.Name,
 pd.UnitPrice,
 od.Quantity,
 od.Price,
 Customer=ct.Name //define anonymous type Customer
 }).ToList();

If the number of tables used is more than that I suggest you to implement a Stored Procedure and use it to get the data. You can still use the EF to call the SPROC and bind the data to any custom DTO class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM