I have looked at similar question - ids not string - but it wont work for me so I am reposting.
Store model:
public class Store
{
public int ID { get; set; }
public virtual IList<Asset> Assets { get; set; }
}
Till:
public class Till
{
public int ID { get; set; }
[ForeignKey("Store")]
public int StoreID { get; set; }
public virtual Store Store { get; set; }
public int Type { get; set; }
public string Kind { get; set; }
public string Name { get; set; }
}
Using MvcCheckBoxList reference library I have a FilterViewModel containing list of names:
public class FilterViewModel
{
public IEnumerable<TillCheckBox> AvailableTills { get; set; }
public IEnumerable<TillCheckBox> SelectedTills { get; set; }
public PostedTills PostedTills { get; set; }
}
public class PostedTills
{
public string[] TillNames { get; set; }
}
The FilterViewModel.AvailableTills is initialised with the names of all assets of type Till (eg. { "Front", "Back", "Floor", "Shop", "Cafe" }):
private List<string> GetAllTills()
{
return (from a in _db.Assets where (a.Kind == "Till") select a.Name).Distinct().ToList();
}
The FilterViewModel.SelectedTills and FilterViewModel.PostedTills are both empty. Nothing selected yet.
When the View uses FilterViewModel and when submits back via GET a list of selected checkboxes back to the controller in FilterViewModel .
What I am trying to do is get list of stores which have assets with name in PostedTills list.
So if user selects "Front" and "Cafe" I want stores which has assets named "Front" AND "Cafe".
Instead what I am getting is a list of stores with assets named "Front" (but not necessarily "Cafe") and assets named "Cafe" (but not "Front").
The Linq query I am trying (based on other post) is:
stores = (from s in db.Stores
join a in db.Assets on s.ID equals a.StoreID
where (a => postedTillNames.Contains(a.Name))
select s).Distinct().ToList();
But this gives an error "Cannot convert lambda expression to type 'bool' because it is not a delegate type" on:
(a => postedTillNames.Contains(a.Name))
I have also tried different queries but cannot get the results I am after.
Anyone able to help please?
Regards Craig