H20rider
H20rider

Reputation: 2282

Group By Multiple Columns - 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

Upvotes: 5

Views: 2893

Answers (1)

Daniel Schaffer
Daniel Schaffer

Reputation: 57822

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.

Upvotes: 12

Related Questions