动态linq by子句

| 我有多个linq查询,它们只是在不同的分组级别上检索相同的数据。 (可能有3个不同级别)。 linq查询当前会生成一个自定义对象的可枚举列表。我不理解或不知道的项目(以减少冗余代码): 我可以使以下group by子句动态化吗? 如果是这样,在该级别进行分组时,是否可以动态填充我的自定义对象组数据。 例如:
 var myReport_GroupProductLevel =
                from r in mySum_GroupProductLevel
                join pc in _myPlotCount on r.Strata equals pc.Strata
                join acr in _myStrataAcres on pc.Strata equals acr.Strata
                group new { r, pc, acr } by new { r.Strata, pc.Count, acr.Acres, r.GroupName, r.ProductName } into g
                select new DataSummary
                {
                    Strata = g.Key.Strata,
                    PlotCount = g.Key.Count,
                    Acres = g.Key.Acres,
                    ClassName = string.Empty,
                    GroupName = g.Key.GroupName,
                    ProductName = g.Key.ProductName,
                    TPAMEAN = g.Sum(x => x.r.TPA / x.pc.Count),
                    TPADEV = g.Select(x => x.r.TPA).StdDev(g.Key.Count)
                };
如果我只想按\“ GroupName \”进行分组...,我将重写查询。我看到的问题是,如果我要按值分组,则在查询中需要该值(g.Key.GroupName);但是由于我正在创建新的自定义对象,因此其他非分组值(例如\“ ClassName \”)需要一个值(我在上面使用了string.Empty,但这是静态的)。 感谢您的任何见解...     
已邀请:
如果有人好奇,我可以通过使用条件语句来使它起作用……因为按空分组会使其崩溃。
var mySum_ClassGroupProductLevel =
                from s in ReportData.myStands
                join p in ReportData.myPlots on s.ID equals p.StandID
                join t in ReportData.myTrees on p.ID equals t.PlotID
                group t by new { s.Strata, p.ID, 
                    ClassName = useClassName ? t.ClassName : string.Empty, 
                    GroupName = useGroupName ? t.GroupName : string.Empty, 
                    ProductName = useProductName ? t.ProductName : string.Empty }
                    into g
                select new
                {}
    

要回复问题请先登录注册