I have an exhibition scenario.
How do I exclude a list of available Stands below, from the drop down list I would show in the Exhibitor view.
In Sql I would:
Select StandID, Description from Stand
Where StandID not in (Select StandID from Exhibitor)
Exhibitors register - and select the stand they want from a dropdown list (StandID/Stand) as per the model below:
public class Exhibitor
{
public int ExhibitorID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public int StandID { get; set; }
public virtual Stand Stand { get; set; }
}
The Stand Model is as follows:
public class Stand
{
public int StandID { get; set; }
public string Description { get; set; }
public bool Booked { get; set; }
}
I can get a list of Stands as follows:
var stands = db.Stands.ToList().Where(s => s.Booked==false)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.StandNumber + ": " + s.Description + ": " + s.Size + ": £" + s.Rate.ToString()
});
But how do I exclude the (Select StandID from Exhibitor) from the stands list?
I tried:
var booked = db.Exhibitors.Select(s => new { s.StandID }).ToList();
then using:
var stands = db.Stands.ToList().Where(s => s.Booked==false)
.Except(booked)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.StandNumber + ": " + s.Description + ": " + s.Size + ": £" + s.Rate.ToString()
});
But that didn't work.
UPDATE
This appears to work - but I'm no expert, so would appreciate any advise on if it;s the best practice way or not?
var stands = db.Stands.ToList().Where(s => !db.Exhibitors
.Any(bk => bk.StandID == s.StandID) && s.Booked == false)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.StandNumber + ": " + s.Description + ": " + s.Size + ": £" + s.Rate.ToString()
});
Thanks for any help?
Mark