Solved

LINQ multiple column grouping

Posted on 2009-06-30
4
3,900 Views
Last Modified: 2013-11-11
Hi Experts,

I am trying to build a statement  where I group on more than one column. I have looked on the web and found this site: http://ddkonline.blogspot.com/2008/04/linq-group-by-syntax-for-grouping-on.html

He supplies this statement in C#:

from f in FundUserRoles
group f by new {f.RoleId, f.UserName}
into myGroup
where myGroup.Count() > 0
select new { myGroup.Key.RoleId, myGroup.Key.UserName, FundCount = myGroup.Count()}

I then tried to make one similar but cannot do it in VB.NET

 Dim test = From ledger In db.ledgers _
                           Group By New {ledger.postingID, ledger.link_posting_id} Into mygroup() Select mygroup.postingId

Wondering if someone might show me the way..

Kind Regards

Simon
0
Comment
Question by:si2030
  • 2
4 Comments
 
LVL 8

Expert Comment

by:bhmahler
ID: 24745393
you don't need all that to group on multiple columns, you just need to group by them

Dim test = From ledger in db.ledgers _
                 Group By ledger.postingID, ledger.link_posting_id _
                 Select ledger.postingID
0
 
LVL 8

Accepted Solution

by:
bhmahler earned 250 total points
ID: 24745565
sorry was having a brain fart there, the post above will not work, here is a working example


Dim test = From ledger in db.ledgers _

           Group ledger by ledger.postingID, ledger.link_posting_id Into g = Group _

           From c in g _

           Select c.postingID

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 24764860
Hi si2030;

When you look at code snippet on the Net you need to have some caution especially with Linq implementation. For example in C# the Group does not have a grouping key list where in Visual Basic the syntax is much different, Group [ Column List] By key1 [, key2 [...] ] Into aggregateList, so it is not that Linq falls short of grouping with multiple columns as the link you posted states but it is just the syntax that each language uses to implement the Linq clause, C# allows you to use the anonymous type to define multiple columns in the Group such as, group f by new {f.RoleId, f.UserName} into myGroup, as the link you posted points out.

If you only wanted one column to be returned from the query then the post made by bhmahler should work. If you want other columns or something different let me know.
 
Fernando
0
 

Author Closing Comment

by:si2030
ID: 31598323
I ended up working this out a different way however the answers above both address the question and thus work...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now