繁体   English   中英

linq查询连接两个表并从另一个表中获取一个表值的计数

[英]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.

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