Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. question about linq query to run in c#

question about linq query to run in c#

Scheduled Pinned Locked Moved LINQ
csharpdatabasequestionlinq
15 Posts 2 Posters 46 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 11570154

    I was trying to add all the cars booked to a collection and all the cars available to a second collection and the comparing the two collection and not listing out the one that match but I can't really get it work thanks for your time cheers

    S Offline
    S Offline
    Sascha Lefevre
    wrote on last edited by
    #6

    You could post your code and I'll try to tell you why it doesn't work. Maybe this pseudo-code helps?:

    foreach car in allCars
    {
    foreach booking in car.Bookings
    {
    if (booking overlaps with requested time frame)
    {
    unavailableCars.Add(car)
    break
    }
    }
    }
    availableCars = allCars.Except(unavailableCars)

    alternatively:

    availableCars = allCars.Except(allCars.Where(car => car.Bookings.Any(bkg => bkg overlaps with requested timeframe)))

    A booking does not overlap with a requested time frame if:

    bookingEndDate < requestStartDate || bookingStartDate > requestEndDate

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    U 2 Replies Last reply
    0
    • S Sascha Lefevre

      You could post your code and I'll try to tell you why it doesn't work. Maybe this pseudo-code helps?:

      foreach car in allCars
      {
      foreach booking in car.Bookings
      {
      if (booking overlaps with requested time frame)
      {
      unavailableCars.Add(car)
      break
      }
      }
      }
      availableCars = allCars.Except(unavailableCars)

      alternatively:

      availableCars = allCars.Except(allCars.Where(car => car.Bookings.Any(bkg => bkg overlaps with requested timeframe)))

      A booking does not overlap with a requested time frame if:

      bookingEndDate < requestStartDate || bookingStartDate > requestEndDate

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      U Offline
      U Offline
      User 11570154
      wrote on last edited by
      #7

      //query to get all the carId make and model booked on those days
      var query1 = from b in db.Bookings
      where b.StartDate >= Start &&
      b.EndDate <= End
      select new { Make = b.Car.Make, Model = b.Car.Model, CarID = b.CarID };

                          //ad all the boked car to a collection called carBooked
                          foreach (var c in query1)
                          {
                              Car car1 = new Car(c.Make, c.Model, c.CarID, Start, End);
                              carSizeTotal.Add(car1);
      
                          }
      
      
                          //query to get all the car available 
                          var query = from c in db.Cars
                                      select new { Make = c.Make, Model = c.Model, CarID = c.ID };
      
      
      
                          //adding all the cars to a collection 
                          foreach (var c in query)
                          {
                              Car car = new Car(c.Make, c.Model, c.CarID, Start, End);
                              carSizeTotal.Add(car);
                          }
      

      does one are the 2 query I have to get all the cars booked and all the cars in the cars table then I just need to nested a foreach loop to eliminate the already bookede

      S 1 Reply Last reply
      0
      • S Sascha Lefevre

        You could post your code and I'll try to tell you why it doesn't work. Maybe this pseudo-code helps?:

        foreach car in allCars
        {
        foreach booking in car.Bookings
        {
        if (booking overlaps with requested time frame)
        {
        unavailableCars.Add(car)
        break
        }
        }
        }
        availableCars = allCars.Except(unavailableCars)

        alternatively:

        availableCars = allCars.Except(allCars.Where(car => car.Bookings.Any(bkg => bkg overlaps with requested timeframe)))

        A booking does not overlap with a requested time frame if:

        bookingEndDate < requestStartDate || bookingStartDate > requestEndDate

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        U Offline
        U Offline
        User 11570154
        wrote on last edited by
        #8
                            LbxAvalCars.ItemsSource = carSizeTotal;
        
                            RentCarDBEntities db = new RentCarDBEntities();
        
        
                            //query to get all the carId make and model booked on those days
                            var query1 = from b in db.Bookings
                                         where b.StartDate >= Start &&
                                         b.EndDate <= End
                                         select new { Make = b.Car.Make, Model = b.Car.Model, CarID = b.CarID };
        
                            //ad all the boked car to a collection called carBooked
                            foreach (var c in query1)
                            {
                                Car car1 = new Car(c.Make, c.Model, c.CarID, Start, End);
                                carBooked.Add(car1);
        
                            }
        
        
                            //query to get all the car available 
                            var query = from c in db.Cars
                                        select new { Make = c.Make, Model = c.Model, CarID = c.ID };
        
        
        
                            //adding all the cars to a collection 
                            foreach (var c in query)
                            {
                                Car car = new Car(c.Make, c.Model, c.CarID, Start, End);
                                carSizeTotal.Add(car);
                            }
        

        sorry this one is correct one the other one has some errors

        S 1 Reply Last reply
        0
        • U User 11570154

          //query to get all the carId make and model booked on those days
          var query1 = from b in db.Bookings
          where b.StartDate >= Start &&
          b.EndDate <= End
          select new { Make = b.Car.Make, Model = b.Car.Model, CarID = b.CarID };

                              //ad all the boked car to a collection called carBooked
                              foreach (var c in query1)
                              {
                                  Car car1 = new Car(c.Make, c.Model, c.CarID, Start, End);
                                  carSizeTotal.Add(car1);
          
                              }
          
          
                              //query to get all the car available 
                              var query = from c in db.Cars
                                          select new { Make = c.Make, Model = c.Model, CarID = c.ID };
          
          
          
                              //adding all the cars to a collection 
                              foreach (var c in query)
                              {
                                  Car car = new Car(c.Make, c.Model, c.CarID, Start, End);
                                  carSizeTotal.Add(car);
                              }
          

          does one are the 2 query I have to get all the cars booked and all the cars in the cars table then I just need to nested a foreach loop to eliminate the already bookede

          S Offline
          S Offline
          Sascha Lefevre
          wrote on last edited by
          #9

          Your first query doesn't accurately return those cars that are booked in the requested time frame. Assuming it would, you then add all booked cars to carSizeTotal and then all cars to carSizeTotal ...that doesn't help, you could as well have skipped adding the booked cars. Take a look at my last message, I think you should be able to figure it out - you're not too far away :)

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          1 Reply Last reply
          0
          • U User 11570154
                                LbxAvalCars.ItemsSource = carSizeTotal;
            
                                RentCarDBEntities db = new RentCarDBEntities();
            
            
                                //query to get all the carId make and model booked on those days
                                var query1 = from b in db.Bookings
                                             where b.StartDate >= Start &&
                                             b.EndDate <= End
                                             select new { Make = b.Car.Make, Model = b.Car.Model, CarID = b.CarID };
            
                                //ad all the boked car to a collection called carBooked
                                foreach (var c in query1)
                                {
                                    Car car1 = new Car(c.Make, c.Model, c.CarID, Start, End);
                                    carBooked.Add(car1);
            
                                }
            
            
                                //query to get all the car available 
                                var query = from c in db.Cars
                                            select new { Make = c.Make, Model = c.Model, CarID = c.ID };
            
            
            
                                //adding all the cars to a collection 
                                foreach (var c in query)
                                {
                                    Car car = new Car(c.Make, c.Model, c.CarID, Start, End);
                                    carSizeTotal.Add(car);
                                }
            

            sorry this one is correct one the other one has some errors

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #10

            Looks like the code you posted first just with the two additional first lines - so my reply should be still valid :)

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            U 1 Reply Last reply
            0
            • S Sascha Lefevre

              Looks like the code you posted first just with the two additional first lines - so my reply should be still valid :)

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              U Offline
              U Offline
              User 11570154
              wrote on last edited by
              #11

              thanks I'll give it a go and see if I can get it work thanks for your help cheers

              S 1 Reply Last reply
              0
              • U User 11570154

                thanks I'll give it a go and see if I can get it work thanks for your help cheers

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #12

                You're welcome! Feel free to ask again if you can't get it to work.

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                U 1 Reply Last reply
                0
                • S Sascha Lefevre

                  You're welcome! Feel free to ask again if you can't get it to work.

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  U Offline
                  U Offline
                  User 11570154
                  wrote on last edited by
                  #13

                  Try all day couldn't get it work

                  S 1 Reply Last reply
                  0
                  • U User 11570154

                    Try all day couldn't get it work

                    S Offline
                    S Offline
                    Sascha Lefevre
                    wrote on last edited by
                    #14

                    Alright.. here you go:

                    LbxAvalCars.ItemsSource = carSizeTotal;

                    RentCarDBEntities db = new RentCarDBEntities();

                    var bookedCars = from b in db.Bookings
                    where b.StartDate <= End && b.EndDate >= Start // disallow same day
                    //where b.StartDate < End && b.EndDate > Start // allow same day
                    select b.Car;

                    var availableCars = db.Cars.Except(bookedCars);

                    foreach (var car in availableCars)
                    {
                    carSizeTotal.Add(new Car(car.Make, car.Model, car.CarID, Start, End));
                    }

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    U 1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      Alright.. here you go:

                      LbxAvalCars.ItemsSource = carSizeTotal;

                      RentCarDBEntities db = new RentCarDBEntities();

                      var bookedCars = from b in db.Bookings
                      where b.StartDate <= End && b.EndDate >= Start // disallow same day
                      //where b.StartDate < End && b.EndDate > Start // allow same day
                      select b.Car;

                      var availableCars = db.Cars.Except(bookedCars);

                      foreach (var car in availableCars)
                      {
                      carSizeTotal.Add(new Car(car.Make, car.Model, car.CarID, Start, End));
                      }

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                      U Offline
                      U Offline
                      User 11570154
                      wrote on last edited by
                      #15

                      thanks really appreciate your help cheers

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups