2

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 }  

has to be constructed dynamically.

Not sure how I can tweak the solution as given at blog 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();
}
ekad
  • 14,436
  • 26
  • 44
  • 46
user2051980
  • 39
  • 1
  • 5
  • 2
    What do you mean by "has to be dynamic?" What isn't working? What is your desired behavior? – zimdanen Feb 07 '13 at 19:06
  • In other words can I replace the code "group r by new { r.Portfolio, r.DataType }" with "group r by new { "groupexpression" }". Where "groupexpression" is the variable which contains the list of all the columns against which I want to do grouping. Some times there can one 1/2/3 columns based on some settings. – user2051980 Feb 07 '13 at 19:24

2 Answers2

2

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.

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

Mike Burton
  • 3,010
  • 24
  • 33
  • Thx Mike. The scott Gui's link was helpfull. Downloaded the DynamicLibraryClass and used as below. var query = lstAllDecSupp.AsQueryable().GroupBy(" new (Portfolio,DataType,ChildPortfolio) ", "it"); – user2051980 Feb 07 '13 at 21:58
2

The following should work with your example, but may not work/scale up very well if your real-life example is more complicated.

// bools to indicate which columns you want to group by 
bool groupByPortfolio      = true;
bool groupByDataType       = true;
bool groupByBucketName     = false;
bool groupByChildPortfolio = false;


List<DecisionSupportData> lstRmgAmt
    = (from r in lstAllDecSupp.AsEnumerable()
       where r.DataType == "P"
       group r by new 
       { 
            Portfolio       = groupByPortfolio      ? r.Portfolio       : null ,
            DataType        = groupByDataType       ? r.DataType        : null , 
            BucketName      = groupByBucketName     ? r.BucketName      : null ,
            ChildPortfolio  = groupByChildPortfolio ? r.ChildPortfolio  : null 
        }
       into gg 

       select new DecisionSupportData
       {
            Portfolio       = gg.Key.Portfolio,
            DataType        = gg.Key.DataType,
            BucketName      = gg.Key.BucketName,
            ChildPortfolio  = gg.Key.ChildPortfolio  

        }
 ).ToList(); 
sgmoore
  • 15,694
  • 5
  • 43
  • 67