Joebocop
Joebocop

Reputation: 569

Datatable Grouping Using Linq and VB .NET

I'm completely dense here, but I'm trying to get some stats from a DataTable. One of the columns in the datatable is called "colour".

I need to find out how many of each instance of "colour" are in the datatable.

I'm trying:

Dim q = From p In PGWorkingDataTable _
            Group p By p("colour") Into Group _
            Select Group

But I get the compiler error at design-time: "Range variable name can be inferred only from a simple or qualified name with no arguments" on the p("colour") section.

I need some serious guidance here. Thanks for your help.

Joe

Upvotes: 2

Views: 12118

Answers (2)

Ian Pugsley
Ian Pugsley

Reputation: 1062

Writing LINQ in VB.NET is nobody's favorite thing. Try something like the following:

Dim q = From p In PGWorkingDataTable _
        Group By colour = p("colour") _
        Into colourCount = Count(p("colour")) _
        Select colour, colourCount

Upvotes: 0

SLaks
SLaks

Reputation: 887413

You need to specify a name for the group key:

From p In new DataTable() _
Group p By Color = p("colour") Into Group _
Select Group

Upvotes: 4

Related Questions