Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Distinct List Items - Sort of

Posted on 2011-03-07
3
Medium Priority
?
837 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

730 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