Hi basically i wanted to group my datatable by the department column, i want to load the results back into another datatable through looping but it keeps giving me an error, im not sure how to group a datatable with linq and load it into another datatable other than looping
here is my code
DataTable data = new DataTable();
data.Columns.Add("Department");
var query1 = dtClone.AsEnumerable().GroupBy(row => row.Field<string>("department"));
if (query1.Any())
foreach(DataRow dr in query1)//here
{
DataRow newrow = data.Rows.Add();
newrow.SetField("Department", dr.Field<string>("department"));
}
foreach (DataRow row in data.Rows)
MessageBox.Show(row[0].ToString());
another linq query i tried
DataTable data = new DataTable();
data.Columns.Add("Department");
var query1 = from r in dtClone.AsEnumerable()
orderby r.Field<string>("department") ascending
group r by r.Field<string>("department") into r
select r;
if (query1.Any())
foreach(DataRow dr in query1)
{
DataRow newrow = data.Rows.Add();
newrow.SetField("Department", dr.Field<string>("department"));
}
var query1 = dtClone.AsEnumerable().Select(row => row.Field<string>("department")).Distinct();