Solved

LINQ question

Posted on 2009-04-06
7
173 Views
Last Modified: 2013-11-11
I have a List object in c#.  I want to group by three fields in that list and test for a duplicate record.  In SQL, it would look like this:

select field1, field2, field3
from Table1
group by field1, field2, field3
having count(*) > 1

Is this possible to do in LINQ?
0
Comment
Question by:dchau12
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 24080337
Is this what you are seeking?

http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/c668f706-3037-4ca6-a5ef-f55c70b00a66
http://ddkonline.blogspot.com/2008/04/linq-group-by-syntax-for-grouping-on.html
http://msdn.microsoft.com/en-us/vbasic/bb688085.aspx
var data = from l in list

           group l by l[0] into gr

           where gr.Count() > 1

           select new { gr };

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24080349
My example is VERY crude--don't read into the variable names, I merely wanted to demonstrate syntax.
0
 

Author Comment

by:dchau12
ID: 24081282
I got it working, and I can see the value that it found, but how do I then mine that value out of "data"?  intelisense only brings up more predicate methods.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:dchau12
ID: 24081423
I can see everything in data2's method "Results View", but how do i access the method?

     var data2 = from f in myList
                        group f by new { f.TrpCarrierId, f.Mode, f.CarrierStatusCode }
                        into myGroup
                        where myGroup.Count() > 1
                        select new
                        {
                          myGroup.Key.TrpCarrierId,
                          myGroup.Key.Mode,
                          myGroup.Key.CarrierStatusCode,
                          MyCount = myGroup.Count()
                        };
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24081593
I believe it would be something like:

foreach (var d in data2)
{
     string.Concat(d.TrpCarrierId, d.Mode);  // etc.
}
0
 

Author Closing Comment

by:dchau12
ID: 31567139
You rock!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24081766
:)

Glad to help.
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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

20 Experts available now in Live!

Get 1:1 Help Now