C# 可重用的LINQ查询,where子句除外
我有一系列电影,它们有各种属性(标题、发行年份、评级等),我需要使用LINQ查询进行搜索,如下所示:C# 可重用的LINQ查询,where子句除外,c#,linq,C#,Linq,我有一系列电影,它们有各种属性(标题、发行年份、评级等),我需要使用LINQ查询进行搜索,如下所示: public BindingList<Movie> SearchByTitle(string title) { var matches = from movies in movieCollection where movies.Title == title select movies; // do
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
}
publicbindinglist SearchByTitle(字符串标题)
{
var matches=来自movieCollection中的电影
where movies.Title==Title
选择电影;
//用火柴做些其他的事情
}
但是我不想用一个单独的方法来搜索每个属性,因为在搜索之间唯一改变的是where
部分。例如where movies.Rating==Rating
或where movies.ReleaseYear==ReleaseYear
。如何通过将某种表达式
或Func
作为where
部分传递,使搜索方法可用于所有不同类型的搜索
如何通过传入某种表达式或Func作为where部分,使搜索方法可用于所有不同类型的搜索
您的查询实际上只是where子句。但您可以轻松地使where部分可配置。。。只是不使用查询表达式
public BindingList<Movie> SearchByTitle(Expression<Func<Movie, bool>> predicate)
{
var matches = movies.Where(predicate);
// Do common stuff with the matches.
}
如何通过传入某种表达式或Func作为where部分,使搜索方法可用于所有不同类型的搜索
您的查询实际上只是where子句。但您可以轻松地使where部分可配置。。。只是不使用查询表达式
public BindingList<Movie> SearchByTitle(Expression<Func<Movie, bool>> predicate)
{
var matches = movies.Where(predicate);
// Do common stuff with the matches.
}
你可以用一个小盒子
检查非常有趣的答案
希望有帮助。你可以用一个
检查非常有趣的答案
希望有帮助。您可以使用扩展方法(在静态类中定义)
公共静态IQueryable AddSearchParameter(此IQueryable查询、bool条件、System.Linq.Expressions.Expression谓词)
{
如果(条件)
{
query=query.Where(谓词);
}
返回查询;
}
例如:
public BindingList<Movie> Search(string title, int? year, int? rating)
{
var matches = movieCollection.AddSearchParameter(!string.IsNullorEmpty(title), m=>m.Title == title);
matches = matches.AddSearchParameter(year.HasValue, m=>m.Year == year.Value);
matches = matches.AddSearchParameter(rating.HasValue, m=>m.rating >= rating.Value);
// do some other stuff with the matches
}
公共绑定列表搜索(字符串标题、整数年、整数评级)
{
var matches=movieCollection.AddSearchParameter(!string.IsNullorEmpty(title),m=>m.title==title);
matches=matches.AddSearchParameter(year.HasValue,m=>m.year==year.Value);
matches=matches.AddSearchParameter(rating.HasValue,m=>m.rating>=rating.Value);
//用火柴做些其他的事情
}
如果对数据库使用此方法,则在枚举之前,它不会实际执行查询,因此不会对数据库进行多次调用。可以使用扩展方法(在静态类中定义此方法)
公共静态IQueryable AddSearchParameter(此IQueryable查询、bool条件、System.Linq.Expressions.Expression谓词)
{
如果(条件)
{
query=query.Where(谓词);
}
返回查询;
}
例如:
public BindingList<Movie> Search(string title, int? year, int? rating)
{
var matches = movieCollection.AddSearchParameter(!string.IsNullorEmpty(title), m=>m.Title == title);
matches = matches.AddSearchParameter(year.HasValue, m=>m.Year == year.Value);
matches = matches.AddSearchParameter(rating.HasValue, m=>m.rating >= rating.Value);
// do some other stuff with the matches
}
公共绑定列表搜索(字符串标题、整数年、整数评级)
{
var matches=movieCollection.AddSearchParameter(!string.IsNullorEmpty(title),m=>m.title==title);
matches=matches.AddSearchParameter(year.HasValue,m=>m.year==year.Value);
matches=matches.AddSearchParameter(rating.HasValue,m=>m.rating>=rating.Value);
//用火柴做些其他的事情
}
如果对数据库使用此函数,则在枚举之前,它实际上不会执行查询,因此不会对数据库进行多次调用。此函数运行良好,但我需要使用
var matches=movies.Where(predicate.Compile())代码>这很好,但我需要使用var matches=movies.Where(predicate.Compile())代码>