LINQ question

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?
dchau12Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
käµfm³d 👽Connect With a Mentor Commented:
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
 
käµfm³d 👽Commented:
My example is VERY crude--don't read into the variable names, I merely wanted to demonstrate syntax.
0
 
dchau12Author Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
dchau12Author Commented:
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
 
käµfm³d 👽Commented:
I believe it would be something like:

foreach (var d in data2)
{
     string.Concat(d.TrpCarrierId, d.Mode);  // etc.
}
0
 
dchau12Author Commented:
You rock!
0
 
käµfm³d 👽Commented:
:)

Glad to help.
0
All Courses

From novice to tech pro — start learning today.