简体   繁体   English

Linq to Entities左外连接分组为一个集合

[英]Linq to Entities Left outer join grouped into a collection

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
}

I have the above LINQ to Entities query that has a LEFT JOIN to get rating values for 1 or more segments for a given component. 我有上面的LINQ to Entities查询,它有一个LEFT JOIN来获取给定组件的1个或多个段的评级值。

The segmentFinanceRating entity has the properties, { MaterialId, SegmentId, Rating, LowRated } segmentFinanceRating实体具有{ MaterialId, SegmentId, Rating, LowRated }属性

At the moment the results are not grouped to the relevant component, ie the SegmentRatings property is not a single collection of segmentFinanceRating objects, instead I have multiple data rows with 1 segmentFinanceRating object in each. 目前,结果未分组到相关组件,即SegmentRatings属性不是segmentFinanceRating对象的单个集合,而是我有多个数据行,每个数据行中包含1个segmentFinanceRating对象。

I have seen some examples of using group x by y into z but I couldn't get it working, possibly due to some of the collections on the component that I need too, I'm not sure. 我已经看到了一些使用group x by y into z例子但是我无法使它工作,可能是由于我需要的组件上的一些集合,我不确定。

Any help would be appreciated on how to do this, thanks. 如果这样做,任何帮助将不胜感激,谢谢。

GroupBy in List doesn't work for you? 列表中的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);

In this case it's much easier to do the join in the anonymous type: 在这种情况下,以匿名类型进行连接要容易得多:

from component in Materials.OfType<Container>().Where(m => m.Active)
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 = (from segmentFinanceRating in segmentFinanceRatingView
                     where segmentFinanceRating.MaterialId == component.Id
                     select segmentFinanceRating)
}

You will have an empty collection of SegmentRatings when there are none for a specific component, giving the same effect as outer join. 当没有特定组件时,您将拥有一个空的SegmentRatings集合,从而产生与外部SegmentRatings相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM