问题描述
我有一个具有属性StockNumber
的对象Style
.我想针对用户在搜索中输入的IEnumerable<string> stockNumbers
过滤所有Db.Styles
的列表. DB.Styles
是IEnumerable<Style>
.本质上,这就是我想要做的事情:
I have an object Style
with a property StockNumber
. I would like to filter a list of all Db.Styles
against an IEnumerable<string> stockNumbers
the user enters in a search. DB.Styles
is an IEnumerable<Style>
. Here is essentially what I am trying to do:
public IEnumerable<Style> LoadListOfStyles(IEnumerable<string> stockNumbers)
{
return Db.Styles.Intersect(stockNumbers);
// Need to filter Db.Styles based on stockNumbers
}
那么,有没有一种简单的方法可以将Db.Styles
中所有样式的列表与stockNumbers
中我的搜索值进行比较,以返回仅用户搜索的Styles
的过滤列表?我看到的能够做到这一点的唯一方法是让2个IEnumerable对象相交,但是从所搜索的库存编号中逐个加载样式似乎是很多不必要的代码.我想知道是否有更简单的方法.还是我需要滚动自己的方法来过滤结果?
So is there an easy way to compare the list of all styles in Db.Styles
against my search values in stockNumbers
to return a filtered list of only the Styles
the user has searched for? The only way I have seen to be able to do this is to have 2 IEnumerable objects to intersect, but loading the styles one by one from the stock numbers searched seems like a lot of unnecessary code. I am wondering if there is an easier way. Or do I need to roll my own method to filter the results?
推荐答案
return db.Styles.Where(style => stockNumbers.Any(sn => sn == style.StockNumber));
这篇关于过滤器IEnumerable< T>针对IEnumerable< string>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!