链接到实体-对多个连接表进行分组

本文关键字:连接 实体 链接 | 更新日期: 2023-09-27 18:02:35

我有一个连接几个表的查询。然后,我需要按前2列分组,然后选择其余的值。现在它对前4列工作得很好,但随后group by停止对所有列工作。我以前使用过类似的查询,它工作得很好,所以如果有人有任何见解,我将不胜感激。

var query = (from p in context.Process
    join pf in context.table on p.ProcessID equals pf.ProcessID
    join f in context.Flows on pf.FlowID equals f.FlowID
    join d in context.Directions on pf.DirectionID equals d.DirectionID
    join ft in context.FlowTypes on f.FlowTypeID equals ft.FlowTypeID
    join fp in context.FlowProperties on f.FlowPropertyID equals fp.FlowPropertyID
    join ug in context.UnitGroups on fp.UnitGroupID equals ug.UnitGroupID
    group new { p, pf, f, d, ft, fp, ug} by new
    {                             
        FlowPropertyName = fp.Name,
        ug.ReferenceUnit,
        FlowDirection = d.FlowDirection,
        FlowType = ft.Type,
        //Won't group on these 3 column
        //ProcessFlowResult =pf.Result,
        //FlowName = f.Name,
        //ProcessFlowID = pf.ProcessFlowID, 
    } into g
    select new Model
    {
        FlowPropertyName = g.Key.FlowPropertyName,
        ReferenceUnit = g.Key.ReferenceUnit,
        FlowDirection = g.Key.FlowDirection,
        FlowType = g.Key.FlowType,
        //ProcessFlowResult = g.Key.ProcessFlowResult,
        //FlowName = g.Key.FlowName,
        //ProcessFlowID = g.Key.ProcessFlowID
    }).AsQueryable();
return query;

链接到实体-对多个连接表进行分组

我明白了。

这是解决方案的缩放lambda版本,使用导航属性;-),以防其他人正在寻找类似的东西。

 var query = context.ProcessFlows
                .GroupBy(p => new
                {
                 FlowPropertyName = p.Flow.FlowProperty.Name
                 })
                .SelectMany(pf => pf.Select(p => new IntermediateFlow
                {
                    FlowPropertyName = p.Flow.FlowProperty.Name,
                    ReferenceUnit = p.Flow.FlowProperty.UnitGroup.Name
                })).AsQueryable();
            return query.OrderBy(pFlow => new { pFlow.FlowPropertyName, pFlow.ReferenceUnit });