Solved

LINQ multiple column grouping

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

19 Experts available now in Live!

Get 1:1 Help Now