动态 linq group by 子句

发布于 2024-11-01 20:15:08 字数 1233 浏览 0 评论 0原文

我有多个 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,但这是静态的)。

感谢您的任何见解...

I have multiple linq queries that retrieve the same data just at different grouping levels. (potentially 3 different levels). The linq query currently results in an enumerable list of a custom object. The items I don't understand or wonder if possible (to reduce redundant code):

can I make the following group by clause to be dynamic?
if so, can it dynamically populate my custom object group data when it is grouped at that level.

For instance:

 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)
                };

If I wanted to group only by "GroupName" instead... I would rewrite the query. Issues I see are, if I'm grouping by a value then I need that value in the query (g.Key.GroupName); but since I'm creating a new custom object the other non-grouped values such as "ClassName" require a value (I used string.Empty above, but that is static).

Thanks for any insight...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2024-11-08 20:15:08

如果有人好奇,我通过使用条件语句让它工作......因为按空分组会使它崩溃。

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
                {}

if anyone was curious, I got it to work by using a conditional statement... since grouping by empty will make it collapse.

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
                {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文