Link to home
Start Free TrialLog in
Avatar of DylanJones1
DylanJones1

asked on

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
Avatar of Yurich
Yurich
Flag of New Zealand image

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
Avatar of Bob Learned
What have you tried with List.Find and predicates?

Bob
Avatar of DylanJones1
DylanJones1

ASKER

This did what I needed.  

 List<User> ThisUserList =  UserNames.FindAll(delegate(User u) {return ThisUser.UserName == u.UserName; });
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial