我有以下 LINQ 示例:
var colorDistribution =
from product in ctx.Products
group product by product.Color
into productColors
select
new
{
Color = productColors.Key,
Count = productColors.Count()
};
所有这些都有效并且非常有意义。
我想要实现的是将其分组为强类型而不是匿名类型。
例如,我有一个 ProductColour 类,我想分组为List<ProductColour>
这可能吗?
谢谢
编辑:好吧,我完全误读了你的帖子。从表面上看,你不想分组by不同的类型 - 您想要投影组中的每个元素into不同的类型。这就是我要做的:
var colorDistributionSql =
from product in ctx.Products
group product by product.Color
into productColors
select
new
{
Color = productColors.Key,
Count = productColors.Count()
};
var colorDistributionList = colorDistributionSql
.AsEnumerable()
.Select(x => new ProductColour(x.Color, x.Count))
.ToList();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)