Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Distinct List Items - Sort of

Posted on 2011-03-07
3
Medium Priority
?
841 Views
Last Modified: 2013-11-11
Given this dataset in a List<CustomItem>, where CustomItem = string Name, int Index, int FID, and a few other random fields:

Name	Index	FID	Misc
user1	100	3	random
user1	200	3	random
user1	100	4	random
user1	200	4	random
user1	300	4	random
user20	100	2	random
user20	200	2	random
user20	300	2	random
user20	100	4	random
user20	200	4	random
user20	300	4	random

Open in new window

I want to extract objects that have the highest Index value, given where Name and FID are equal.

From the above set, I would want these rows:

Name	Index	FID	Misc
user1	200	3	random
user1	300	4	random
user20	300	2	random
user20	300	4	random

Open in new window

I'm using a non-LINQ method now whereby I start with the first record and iterate through the list doing a manual comparison of line by line to see if I should add it to my resultset.

The LINQ .Distinct doesn't seem to be what I want.  I think it's going to be some combinations of group by's and max and let's, but I'm kinda swimming in unfamiliar territory with this one.
0
Comment
Question by:wegee2
  • 2
3 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 35059849
Hi wegee2;

The following query should give you the desired results.

var result = from ci in customItem
             group ci by new { ci.Name, ci.FID } into qGroup
             let Max = qGroup.Max( i => i.Index )
             from rec in qGroup
             where Max == rec.Index
             select rec;

foreach( var ci in result )
{
    string msg = String.Format( "{0,8} {1,4} {2,3} {3,8}", ci.Name, ci.Index, ci.FID, ci.Misc );
    Console.WriteLine( msg );
}

Open in new window


Fernando
0
 

Author Closing Comment

by:wegee2
ID: 35059993
Fernando,

Spot on.  Not only did that work, the results are lightning fast over a large list.  

Thank you.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 35060060
Not a problem, glad to be of help.  ;=)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Loops Section Overview
Suggested Courses

885 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