locked
find item in generic list (net 2) RRS feed

  • Question

  • User1130413779 posted

    i have a calander events that show the event for each day.

     

    because I dont want connect the DB for each day I make sql query that return me all the events for the month

    now I want run for each day and get the events for the day but I didnt success to filter the generic list

    here is my code:

            DateTime fromDate = DateTime.Now;
            DateTime toDate = fromDate.AddMonths(1);
            List<Event> l = geshtop.Biz.Events.GetData(fromDate);
            for (DateTime d = fromDate; d < toDate; d=d.AddDays(1))
            {
                List<Event> lDate = l.FindAll(????)
            }

     

    how can i do it?

    thanks!

     

    Saturday, August 23, 2008 2:46 PM

Answers

  • User-345568039 posted

    If you look again my solution do not loop through all of them each time. The while loop is never run more then the number of events that day. So if you have 30 events in the month the while loop is only supposed to run 30 times. If you have 10 events then it should run 10 times. This is far better then you will get by FindAll.

    FindAll loops through each event in your list every time. Taken from msdn:

    Remarks

    The Predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the Predicate delegate, and the elements that match the conditions are saved in the returned List.

    This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

    So actually you will get better performance from my solution. The trick is that you have to order the list once. The best time is when you pull the events from the database. And they should be in ascending order as I mentioned. If you are sorting on date but descending there is a method called reverse that is quite cheap that you could use.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 24, 2008 7:47 AM

All replies

  • User-345568039 posted

     Instead of filtering the list make sure it is ordered by ascending dates. Because you have only  selected events with dates that either is the same or later then the first day you will hit in your loop you can add a pointer outside the forLoop.

    int eventIndex = 0;

    And inside the for you add a while loop looking like this:

    while(true){

        if( l(eventIndex) [Is a day later then] d){

           break;

        } else{

           //Do what you want with the date because it belong to the day

           eventIndex++;

        } 

    Now you probably should add check to make sure eventIndex doesn't get larger then (l.Count -1). I haven't tested the code so there might be some misstakes here,  also it might be possible that you need to check that the date of the Event is not smaller then the date of the day. I don't think that is needed but you never know.

     Hope that get's you on track / Mikael

    PS. If you are running .net 3.5 you could use linq for the filtering. It would require less code then what I did above.
     

    Sunday, August 24, 2008 1:36 AM
  • User-863835478 posted
    What is "geshtop.Biz.Events.GetData"?
    Sunday, August 24, 2008 1:37 AM
  • User1130413779 posted

     run with loop its the last way that i want for each row run on all the list its a bad solution,

    for now i resloce it using delegete:

      List<Event> resultsList = l.FindAll(delegate(Event ev) { return ev.EventDate.Date == d.Date; });

     

    (geshtop.Biz.Events.... it my business layer to get the data from the DB)

    Sunday, August 24, 2008 2:27 AM
  • User-345568039 posted

    If you look again my solution do not loop through all of them each time. The while loop is never run more then the number of events that day. So if you have 30 events in the month the while loop is only supposed to run 30 times. If you have 10 events then it should run 10 times. This is far better then you will get by FindAll.

    FindAll loops through each event in your list every time. Taken from msdn:

    Remarks

    The Predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the Predicate delegate, and the elements that match the conditions are saved in the returned List.

    This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

    So actually you will get better performance from my solution. The trick is that you have to order the list once. The best time is when you pull the events from the database. And they should be in ascending order as I mentioned. If you are sorting on date but descending there is a method called reverse that is quite cheap that you could use.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 24, 2008 7:47 AM