[英]Subquery in LINQ that's in the select statement, not the where clause
I need to do something like the following 我需要做类似以下的事情
SELECT p.name,
(SELECT COUNT(p.id) FROM products WHERE products.parent_id = p.id) AS sub_products
FROM products AS p
I see lots of LINQ examples of subqueries in the where clause,but nothing like this where it's in the select statement. 我在where子句中看到了很多子查询的LINQ示例,但是在select语句中没有这样的例子。
This query should be equivalent: 此查询应该是等效的:
var query = Products.Select(p => new {
p.Name,
SubProducts = Products.Count(c => c.parent_id == p.id)
});
foreach (var item in query)
{
Console.WriteLine("{0} : {1}", item.Name, item.SubProducts);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.