加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:03:09 所属栏目:百科 来源:网络整理
导读:from component in Materials.OfTypeContainer().Where(m = m.Active)join segmentFinanceRating in segmentFinanceRatingView on component.Id equals segmentFinanceRating.MaterialId into segmentFinanceRatingGroupfrom segmentFinanceRatingWithDefaul
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对象.

我已经看到了一些使用x组进入z的例子但是我无法使它工作,可能是由于我需要的组件上的一些集合,我不确定.

如果这样做,任何帮助将不胜感激,谢谢.

解决方法

列表中的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,SegmentRatings = segmentFinanceRatingWithDefault
}).ToList().GroupBy(s=> s.SegmentRatings);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读