¿Linq a DataSet, agrupado anidado por?

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 aquí.

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.

preguntado el 08 de noviembre de 11 a las 19:11

1 Respuestas

You cannot define property names of anonymous types as strings:

I have defined two classes to get your info together:

internal class Entry
    public string SystemUser { get; set; }
    public string HostName { get; set; }
    public string DIMSUser { get; set; }
    public string EntryDetails { get; set; }
    public string AssetName { get; set; }
    public string EntryDate { get; set; }
    public string EntryTime { get; set; }

internal class UserEntryPerDateInfo
    public string DIMSUser { get; set; }
    public string EntryDate { get; set; }
    public IEnumerable<Entry> EntryList { get; set; }

Here you populate your data:

IEnumerable<Entry> dataList = from d in dataTableResults.AsEnumerable()
                              select new Entry
                                    HostName=d["Host Name"],
                                    DIMSUser=d["DIMS User"],
                                    EntryDetails=d["Entry Details"],
                                    AssetName=d["Asset Name"],
                                    EntryDate=d["Entry Date"],
                                    EntryTime=d["Entry Time"],

Then you group by date and user and get them in another class:

IEnumerable<UserEntryPerDateInfo> result = from a in dataList
                                           group a by new {User = a.DIMSUser, EntryDate = a.EntryDate}
                                           into grouped
                                           new UserEntryPerDateInfo
                                               DIMSUser = grouped.Key.User,
                                               EntryDate = grouped.Key.EntryDate,
                                               EntryList = grouped

respondido 09 nov., 11:15

Thanks, its much closer to working now. I'm still recieving the error "Could not find an implementation of the query pattern for source type 'object'. 'GroupBy' not found". For the piece of code: from o in d["Entry Date"] - rreeves

What do you want to get in DateGroup? What do you get from d["EntryDate"] ? - Ufuk Hacıoğulları

the Date, they are all from the same data table, I just want it grouped by user and date. - rreeves

You want to group them by user or date? - Ufuk Hacıoğulları

both, user first then date, is that possible ? - rreeves

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.