I’m trying to run a Linq query on a datatable to produce a result, filtered by User and then into Date. I’m following an example of the Group By – Nested on the Linq 101 example page here.
However I wasn’t able to get it working. I’m getting a couple of errors. So I was hoping that someone would give me a hand creating the query, or at least help me get on the right track.
This is the current non-working code that I put together from the example.
var results =
from d in dataTableResults.AsEnumerable()
select new
{
d["DIMS User"],
DateGroup =
from o in d["Entry Date"]
group o by o["Entry Date"] into dg
select new { "Entry Date" = dg.Key, "DIMS user" = dg}
};
This is my DataTable Structure: (Date/Time are seperate from ShortDateToString/ShortTimetoString parse)
LogData.Columns.Add("System User", typeof(string));
LogData.Columns.Add("Host Name", typeof(string));
LogData.Columns.Add("DIMS User", typeof(string));
LogData.Columns.Add("Entry Details", typeof(string));
LogData.Columns.Add("Asset Name", typeof(string));
LogData.Columns.Add("Entry Date", typeof(string));
LogData.Columns.Add("Entry Time", typeof(string));
The errors I’m receiving are:
1) Invalid anonymous type member declarator. Anonymous type members
must be declared with a member assignment, simple name or member
access.2) Could not find an implementation of the query pattern for source
type ‘object’. ‘GroupBy’ not found.
Lastly, Thanks for reading my post and Thanks in Advance for the help.
You cannot define property names of anonymous types as strings:
I have defined two classes to get your info together:
Here you populate your data:
Then you group by date and user and get them in another class: