Solved

Distinct List Items - Sort of

Posted on 2011-03-07
3
822 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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
Fernando,

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

Thank you.
0
 
LVL 62

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

11 Experts available now in Live!

Get 1:1 Help Now