Solved

Distinct List Items - Sort of

Posted on 2011-03-07
3
830 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
[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
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 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 63

Expert Comment

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

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

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