本文介绍了过滤器IEnumerable< T>针对IEnumerable< string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有属性StockNumber的对象Style.我想针对用户在搜索中输入的IEnumerable<string> stockNumbers过滤所有Db.Styles的列表. DB.StylesIEnumerable<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&lt; T&gt;针对IEnumerable&lt; string&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:35