SELECT中的LINQ中的子查询,而不是where子句 [英] Subquery in LINQ that's in the select statement, not the where clause

查看:151
本文介绍了SELECT中的LINQ中的子查询,而不是where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做类似以下的事情

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

我在where子句中看到很多LINQ子查询示例,但是在select语句中却没有类似的子查询.

I see lots of LINQ examples of subqueries in the where clause,but nothing like this where it's in the select statement.

推荐答案

此查询应等效:

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);
}

这篇关于SELECT中的LINQ中的子查询,而不是where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆