My question is: how can I combine all of this into one LINQ expression that returns a datatable with the FileName and four counts. Here is how I’m doing it now:
I have a table with the columns FileName and Success/Fail. The Success/Fail column can have the following values:
Success
Fail: Business Exception
Fail: System Exception
I want a datatable that looks like this:
Currently what I’m doing is looping through unique values of FileName and calculating the counts, then adding a data row
(From d in emailSummaryDT.AsEnumerable
Group d by k=d("FileName").toString.Trim into grp=Group
Let sc = grp.Where(Function (g1) g1("Success/Fail").toString.Trim.Equals("Success")).Count
Let bc = grp.Where(Function (g2) g2("Success/Fail").toString.Trim.Equals("Fail: Business Exception")).Count
Let ac = grp.Where(Function (g3) g3("Success/Fail").toString.Trim.Equals("Fail: System Exception")).Count
Let tc = grp.Count
Let ra = new Object(){k,sc,bc,ac, tc}
Select r = dtResult.Rows.Add(ra)).CopyToDataTable
Just to start with a raw snippet which we could also make more beautiful
Just to make sure I understand how LINQ works when grouping, the “Group by…into grp=Group” statement essentially loops over the unique values (ie the group), executing the Let statements for each group?