![](/img/trans.png)
[英]Get all tables from database and loop through each table using C#, Linq query and Entity Framework
[英]Is it possible to get result from joined SQL query from several tables in C# with Entity Framework?
我正在使用.Net Core 與 C# 和實體框架。 我需要獲取用於報告的數據,它需要連接多個表。
我的問題是,調用 SQL 查詢並將結果傳遞給客戶端應用程序的最佳方法是什么?
如果使用的表數量少於 3 或 4,我們可以使用 EF 連接從多個表中獲取數據。
樣品 linq 加入
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();
如果使用的表數量超過我建議您實現存儲過程並使用它來獲取數據。 您仍然可以使用 EF 調用 SPROC 並將數據綁定到任何自定義 DTO class。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.