Group By multiple Columns in DataTable using LINQ in C# and VB.Net

Last Reply 5 months ago By SUJAYS

Posted 5 months ago

I am not asking like that . This is my datatable 

DataTable dt = GetDataTableFromExcel();
List<DataTable> dts = dt.AsEnumerable()
                .GroupBy(row => row.Field<string>("OUTLET NAME "))
                .Select(g => g.CopyToDataTable()).ToList();

As it is selecting based on Outlet name, here i want to select based another column too using C# with Linq query. I tried like this but not working.

DataTable dt = GetDataTableFromExcel();
List<DataTable> dts = dt.AsEnumerable()
                .GroupBy(row => row.Field<string>("OUTLET NAME ","Occasion"))
                .Select(g => g.CopyToDataTable()).ToList();

 

You are viewing reply posted by: SUJAYS 5 months ago.
Posted 5 months ago

I got the ouput from this.

var result = from rows in dt.AsEnumerable()
             group rows by new { OUTLETNAME = rows["OUTLET NAME "], Occasion = rows["Occasion"] } into grp
             select grp;