Alon Shmiel
Alon Shmiel

Reputation: 7121

Group By Multiple columns and count it

I have a list (sourceList), for example:

id      price1    price2    Data
---------------------------------
12       88        222        1
12       88        222        4
12       66        234        4

I need to group it by all the three columns: id, price1, price2.

There is no meaning for the column Data.

I need to get this new list:

id    numOfAds    price1    price2
----------------------------------
12       2          88        222   -> 2 cause there are two simmilar ads with the same price 1 and 2
12       1          66        234

so I tried:

Dim oResult = sourceList.GroupBy(Function(v) New With 
              {Key v.id, Key v.price1, Key v.price2}).ToList()

But I don't know how to count the equal rows..

EDITED:

Maybe something like this one?

Dim sums = sourceList.GroupBy(Function(x) New With {Key x.id, Key 
            x.price1, Key x.price2}) _
            .Select(Function(group) New With {Key .Peo = group, Key .NumOfAds = group.Count()})

Any help appreciated!

Upvotes: 0

Views: 727

Answers (1)

U r s u s
U r s u s

Reputation: 6978

I think your problem might be the New with statements.

Try this

Dim sums = sourceList.AsEnumerable().GroupBy(Function(x) x.Field(Of String)("YourField")).Where(Function(g) g.Count() > 1).Select(Function(g) g.Key)

This SO thread might also be helpful for what you need.

Upvotes: 1

Related Questions