Solved

LINQ multiple column grouping

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

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…

821 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