C# List.Find  and Predicates - How To

Posted on 2007-08-08
Last Modified: 2008-01-09
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.

Question by:DylanJones1
    LVL 21

    Expert Comment

    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,
    LVL 96

    Expert Comment

    by:Bob Learned
    What have you tried with List.Find and predicates?


    Author Comment

    This did what I needed.  

     List<User> ThisUserList =  UserNames.FindAll(delegate(User u) {return ThisUser.UserName == u.UserName; });
    LVL 1

    Accepted Solution

    PAQed with points refunded (500)

    EE Admin

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    731 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

    17 Experts available now in Live!

    Get 1:1 Help Now