Solved

Distinct List Items - Sort of

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

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

8 Experts available now in Live!

Get 1:1 Help Now