Solved

LINQ question

Posted on 2009-04-06
7
172 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 74

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 74

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 74

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 74

Expert Comment

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

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

18 Experts available now in Live!

Get 1:1 Help Now