Solved

LINQ question

Posted on 2009-04-06
7
177 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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