4

How can I create LINQ to SQL request where I can use group by with condition?

For example:

    from ri in resItems
    group ri by new {groupByPackaging ? (ri.Model, ri.Condition, ri.Packaging) : (ri.Model, ri.Condition)}
    into g
        select new
        {
            ...
        }
misho
  • 1,195
  • 6
  • 16
  • 29

1 Answers1

6

I think this is what your looking for LINQ Conditional Group

Here's an example:

bool someFlag = false;
var result = from t in tableName
      group t by new { FieldA = (someFlag ? 0 : t.FieldA), t.FieldB } into g
      select g;
Community
  • 1
  • 1
Gage
  • 7,365
  • 9
  • 47
  • 77