如下我有具有各種屬性,我需要搜索使用LINQ查詢電影(標題,發行年份,等級,等等)的集合:可重複使用LINQ查詢除了where子句
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
部分來對所有不同類型的搜索重複使用搜索方法?
這很好,但我需要使用'var matches = movies.Where(predicate.Compile());' – Brett
@Brett:將編輯這個... –