我收集了一组具有各种属性(标题、发行年份、评级等)的电影,我需要使用 LINQ 查询来搜索这些属性,如下所示:
public BindingList<Movie> SearchByTitle(string title)
{
var matches = from movies in movieCollection
where movies.Title == title
select movies;
// do some other stuff with the matches
}
但是我不希望使用单独的方法来搜索每个属性,因为在搜索之间唯一改变的是该where部分。例如where movies.Rating == rating或where movies.ReleaseYear == releaseYear。如何通过传递某种Expression或Func作为where部分来使搜索方法可重复用于所有不同类型的搜索?