gpt4 book ai didi

c# - Linq to Entities Left outer join分组到一个集合中

转载 作者:行者123 更新时间:2023-11-30 12:45:47 26 4
gpt4 key购买 nike

from component in Materials.OfType<Container>().Where(m => m.Active)
join segmentFinanceRating in segmentFinanceRatingView on component.Id equals segmentFinanceRating.MaterialId into segmentFinanceRatingGroup
from segmentFinanceRatingWithDefault in segmentFinanceRatingGroup.DefaultIfEmpty()
select new
{
id = component.Id,
name = component.Name,
subType = component.SubType,
size = component.Size,
MaterialIds = component.Materials.Select(x => x.Id),
BrandNames = component.Brands.Select(x => x.Name),
SegmentRatings = segmentFinanceRatingWithDefault
}

我有上面的 LINQ to Entities 查询,它有一个 LEFT JOIN 来获取给定组件的 1 个或多个段的评级值。

segmentFinanceRating 实体具有属性,{ MaterialId, SegmentId, Rating, LowRated }

目前结果没有分组到相关组件,即 SegmentRatings 属性不是 segmentFinanceRating 对象的单个集合,而是我有多个数据行每个中有 1 个 segmentFinanceRating 对象。

我看过一些使用 group x by y into z 的例子,但我无法让它工作,可能是因为我也需要组件上的一些集合,我是不确定。

如有任何帮助,我们将不胜感激,谢谢。

最佳答案

List 中的 GroupBy 不适合你?

var list = (from component in Materials.OfType<Container>().Where(m => m.Active)
join segmentFinanceRating in segmentFinanceRatingView on component.Id equals segmentFinanceRating.MaterialId into segmentFinanceRatingGroup
from segmentFinanceRatingWithDefault in segmentFinanceRatingGroup.DefaultIfEmpty()
select new
{
id = component.Id,
name = component.Name,
subType = component.SubType,
size = component.Size,
MaterialIds = component.Materials.Select(x => x.Id),
BrandNames = component.Brands.Select(x => x.Name),
SegmentRatings = segmentFinanceRatingWithDefault
}).ToList().GroupBy(s=> s.SegmentRatings);

关于c# - Linq to Entities Left outer join分组到一个集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22939589/

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