Solved

LINQ multiple column grouping

Posted on 2009-06-30
4
3,913 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 39
Help with error when uploading excel file 3 26
get row value in vb.net 4 14
location of a form 2 10
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

815 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

9 Experts available now in Live!

Get 1:1 Help Now