score:3

Accepted answer

Use the Linq Contains() method

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contains.aspx

var booked = db.Exhibitors.Select(s => s.StandID).ToList();
var stand = db.Stands.Where(s=>s.Booked == false 
                                       && !booked.Contains(s.StandId)
              .Select(s => new SelectListItem
              {
                  Value = s.StandID.ToString(),
                  Text = s.StandNumber + ": " + s.Description + ": " + s.Size + ": £" + s.Rate.ToString()
              });

I see you updated your question about best practices, too. There's a lot of optimization that can be done to your technique. First, read about the differences between IEnumerable and IQueryable.

Specifically speaking, you should remove the ToList() from Stands.ToList().Where(). By performing ToList() before Where() you're bringing back all of your rows from the db, and then processing them in memory. This could be very costly. If you perform Stands.Where(...) then your filtering will be done in sql and only the rows you want will be returned.

Generally, save your ToList() to be your final step, and actively decide if you need the list to be constructed at all.

score:0

Ideal way is always to create a model that you want as output, map the output to that model and return it. This will also help the client side to get a definitive model to stick to and implement.

Also complex select queries in LINQ are generated on the fly which takes more time than a well structured stored procedure, which is pre-compiled. Consider this if you are expecting faster responses.


Related Articles