c# – Group By Multiple Columns

c# – Group By Multiple Columns

Use an anonymous type.

Eg

group x by new { x.Column1, x.Column2 }

Procedural sample:

.GroupBy(x => new { x.Column1, x.Column2 })

c# – Group By Multiple Columns

Ok got this as:

var query = (from t in Transactions
             group t by new {t.MaterialID, t.ProductID}
             into grp
                    select new
                    {
                        grp.Key.MaterialID,
                        grp.Key.ProductID,
                        Quantity = grp.Sum(t => t.Quantity)
                    }).ToList();

Leave a Reply

Your email address will not be published.