Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

LINQ multiple column grouping

Posted on 2009-06-30
4
Medium Priority
?
4,041 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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:…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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