I have an object Style
with a property StockNumber
. I would like to filter a list of all Db.Styles
against an IEnumerable
public IEnumerable<Style> LoadListOfStyles(List<string> stockNumbers)
{
return db.Styles.Where(s => stockNumbers.Contains(s.StockNumber));
}
Consider a Where
clause.
public IEnumerable<Style> LoadListOfStyles(IEnumerable<string> stockNumbers)
{
return Db.Styles.Where(style => !stockNumbers.Contains(style.StockNumber));
}
return db.Styles.Where(style => stockNumbers.Any(sn => sn == style.StockNumber));
You can think of Intersect
as effectively a Join
where the keys of each object are always "themselves". Here you want to perform a join where the key isn't always "itself", so you use a Join
(or, if you need to remove duplicates, a GroupJoin
).
var query = from style in Db.Styles
join number in stockNumbers
on style.StockNumber equals number
into numbers
where numbers.Any()
select style;