locked
c# how to search through List RRS feed

  • Question

  • User1368407367 posted

    hi i've created a list object and insert some data (date, time, event)

    i want to ristrict so that date and time are unique

     

    for example

     

    xyz.bookedList =new List<Booking>();

    xyz.bookedList.Add(bookingxyz);

     

    //bookingxyz contains data (date, time, event)

     

    How can i ristrict so that only one unique data/time event could be addedd

    if not, how can i search through what i have entered to make sure i dont add the same?

     

     

     

    Saturday, August 10, 2013 8:09 PM

Answers

  • User-1360095595 posted

    You can use a linq query to search your list for the existence of the date. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 10, 2013 10:47 PM
  • User-1910946339 posted

    Don't use a List, use Dictionary<DateTime, Booking>

    xyz.Bookings = new Dictionary<DateTime,Booking>();

    To add a booking

    xyz.Bookings[theBooking.When] = theBooking;

    To see if you have a booking on a given date

    if (xyz.Bookings.ContainsKey(theDateIAmInterestedIn)) {  Already have a booking }

    To iterate through all the bookings

    foreach(var booking in xyz.Bookings.Values) { booking is of type Booking}

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 11, 2013 12:58 AM
  • User1356206773 posted

    If you want to use List , write code as below-

    bool flag = false;
    foreach (var bookingItem in xyz.bookedList)
    {
    /* [It is algo code]
    IF  data (date, time, event) of current bookingItem is equal to your new  data (date, time, event) to add in list ( let it is- bookingxyz) THEN
    SET flag = true;

    break loop iteration.
    END
    */
    }

    if (flag == false)
    {
    xyz.bookedList.Add(bookingxyz);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 11, 2013 12:55 PM

All replies

  • User-1360095595 posted

    You can use a linq query to search your list for the existence of the date. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 10, 2013 10:47 PM
  • User-1910946339 posted

    Don't use a List, use Dictionary<DateTime, Booking>

    xyz.Bookings = new Dictionary<DateTime,Booking>();

    To add a booking

    xyz.Bookings[theBooking.When] = theBooking;

    To see if you have a booking on a given date

    if (xyz.Bookings.ContainsKey(theDateIAmInterestedIn)) {  Already have a booking }

    To iterate through all the bookings

    foreach(var booking in xyz.Bookings.Values) { booking is of type Booking}

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 11, 2013 12:58 AM
  • User1356206773 posted

    If you want to use List , write code as below-

    bool flag = false;
    foreach (var bookingItem in xyz.bookedList)
    {
    /* [It is algo code]
    IF  data (date, time, event) of current bookingItem is equal to your new  data (date, time, event) to add in list ( let it is- bookingxyz) THEN
    SET flag = true;

    break loop iteration.
    END
    */
    }

    if (flag == false)
    {
    xyz.bookedList.Add(bookingxyz);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 11, 2013 12:55 PM