Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group By Multiple Columns - LINQ

Tags:

c#

linq

I have seen examples of multiply group by columns, but for classes. I am trying to do this for a EnumerableDataRowList. but I get "Invalid anonymous type member declarator."

                    EnumerableDataRowList<DataRow> enumerableRowCollection = new EnumerableDataRowList<DataRow>(reportData.Select("WeekKey <> '0'"));


                var groupedRows1 = from row in enumerableRowCollection
                                  group row by new {row["NETWORK"], row["Week"] };

also, I seen some people combine the columns in some case to get the same results. Any benefit in doing it that way

like image 371
H20rider Avatar asked May 19 '11 16:05

H20rider


1 Answers

You have to assign an identifier to the values:

         var groupedRows1 = from row in enumerableRowCollection
                            group row by new { Network = row["NETWORK"], Week = row["Week"] };

You normally aren't required to specify an identifier if you're using a field or property reference as it'll just reuse the name of that member - but in this case you're accessing an indexer property, so there's no way to get a name from it.

like image 195
Daniel Schaffer Avatar answered Oct 11 '22 12:10

Daniel Schaffer