LINQ - check if ALL datas of a return Array match condition that is a string

Asked By Lizzie on 13-Jan-11 06:18 AM
Dear all,

I need your urgent help!!!

I have a query which returns an Array of values. How could i check if all datas in that array match my condition which is a string so i could update another stuff?

public void CheckoutRoom(string bookid, string room)
 {
      var myQuery =    from rsvn in db.Reservations
                  join BRN in db.BookingRoomNos on rsvn.BookingId equals BRN.BookingId
                  join RD in db.RoomDetails on BRN.RoomNo equals RD.RoomNo
                  where rsvn.BookingId == bookid
                  select RD;

      foreach (RoomDetail rd in myQuery)
        {
          if (rd.RoomStatus == "Vacant")
          {
            Reservation res = db.Reservations.Single(re => re.BookingId == bookid);
            res.BookingStatus = "Vacant";
            db.SubmitChanges();
          }
        }
     db.SubmitChanges();
}

i understand here for the above code if only one data of the Array has value of Vacant exist the codes of IF will run. While what i want is if ALL of them match "Vacant" the IF code will run. I need it in urgent!

Thank you!!
Indranil Chatterjee replied to Lizzie on 14-Jan-11 05:22 AM
You can apply the same principle as before:
//Get data for myQuery as you've done in your code and then write this code

if(myQuery.All(rd => rd.RoomStatus == "Vacant"))
{
  Reservation res = db.Reservations.Single(re => re.BookingId == bookid);
  res.BookingStatus = "Vacant";
  db.SubmitChanges();
}

//LINQ All Operator checks whether all items of a sequence satisfies a condition supplied in its argument. Hope that helps.