[英]linq query to join two tables and get the count from one table values from the other
我有兩個表客戶,訂單
客戶 CustomerID FName LName
訂單 OrderId CustomerID OrderDate
我想創建一個linq語句,可以加入這兩個表並得到
FName,LName,每個客戶的訂單數量
from c in Customers
join o in Orders on c.CustomerID equals o.CustomerID into g
select new { c.FName, c.LName, Count=g.Count() }
from c in db.Customers
let theCount = c.Orders.Count()
select new {c.FName, c.LName, theCount}
http://msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic11
這些訪問操作轉換為等效SQL中更復雜的連接或相關子查詢,允許您在查詢期間遍歷對象圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.