C# List.Find and Predicates - How To

I have a list of users that includes name, startdate, and enddate.  A user may be listed several times but with diferent start and end dates.   I want to create a new list that contains a single entry for each user with that users minimum startdate and maximum enddate.    I am using C# and attempting to complete this task using   List.Find  and Predicates but not having much success.  ANy help greatly aprreciated.

TIA
DylanJones1Asked:
Who is Participating?
 
Computer101Connect With a Mentor Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
 
YurichCommented:
I would create a new data source use a SQL query similar to the following (it can be in a stored procedure, doesn't matter):

SELECT User,
(
  SELECT Top 1 start_date   -- earliest start_date
  FROM YourDates
  WHERE User = u1.User
  ORDER BY start_date
) as start_date,
(
  SELECT Top 1 end_date   -- latest end_date
  FROM YourDates
  WHERE User = u1.User
  ORDER BY end_date DESC
) as end_date

FROM Users as u1

It can vary a bit depending how you exactly store your data (e.g. User can have UserID PK, not just User, etc.) but that would be the fastest I think.

Good luck,
Yurich
0
 
Bob LearnedCommented:
What have you tried with List.Find and predicates?

Bob
0
 
DylanJones1Author Commented:
This did what I needed.  

 List<User> ThisUserList =  UserNames.FindAll(delegate(User u) {return ThisUser.UserName == u.UserName; });
0
All Courses

From novice to tech pro — start learning today.