使用LINQ进行动态分组 [英] Dynamic Grouping Using LINQ

查看:158
本文介绍了使用LINQ进行动态分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的例子。集团条款必须是动态的。你可以指导我怎么做到这一点。即行

Please have a look at below example. The Group Clause has to be dynamic. Can you please guide me how this can be achieved. i.e. the row

{ r.Portfolio, r.DataType }  

必须动态构建。

不知道如何调整博客 http://jonahacquah.blogspot.com/2012/02/ groupby-multiple-columns-using-dynamic.html

public class DecisionSupportData
{
    public string Portfolio { get; set; }
    public string BucketName { get; set; }
    public string DataType { get; set; }
    public string ChildPortfolio { get; set; }
}

public void PopulateData()
{
    List<DecisionSupportData> lstAllDecSupp = decisionSupportDataBindingSource.DataSource as List<DecisionSupportData>;
    List<DecisionSupportData> lstRmgAmt
        = (from r in lstAllDecSupp.AsEnumerable()
           where r.DataType == "P"
           group r by new { r.Portfolio, r.DataType } into gg
           select new DecisionSupportData
           {
               DataType = gg.Key.DataType,
               Portfolio = gg.Key.Portfolio,
           }).ToList();
}


推荐答案

DynamicLinq库将显示为解决您的问题,如 Scott Gu的原创博客。只需使用带有字符串值的GroupBy扩展方法即可。

The DynamicLinq library would appear to solve your issue, as mentioned in Scott Gu's original blog. Just use the GroupBy extension method with a string value.

或者你可以挖掘他们的ExpressionParser类,看看它在做什么。

Or you could dig into their ExpressionParser class and see what it's doing.

这篇关于使用LINQ进行动态分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆