gpt4 book ai didi

c# - LINQ 中的子查询位于 select 语句中,而不是 where 子句中

转载 作者:太空狗 更新时间:2023-10-29 23:35:29 25 4
gpt4 key购买 nike

我需要做如下的事情

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 语句中没有像这样的地方。

最佳答案

这个查询应该是等价的:

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

关于c# - LINQ 中的子查询位于 select 语句中,而不是 where 子句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2098375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com