【发布时间】:2010-10-14 19:50:00
【问题描述】:
有点卡在这上面。基本上我有一个方法,我想返回一个可以用作 Where 条件的谓词表达式。 我认为我需要做的与此类似:http://msdn.microsoft.com/en-us/library/bb882637.aspx 但我对我需要做的事情有点困惑。
方法:
private static Expression<Func<Conference, bool>> GetSearchPredicate(string keyword, int? venueId, string month, int year)
{
if (!String.IsNullOrEmpty(keyword))
{
// Want the equivilent of .Where(x => (x.Title.Contains(keyword) || x.Description.Contains(keyword)));
}
if (venueId.HasValue)
{
// Some other predicate added...
}
return ??
}
示例用法:
var predicate = GetSearchPreducate(a,b,c,d);
var x = Conferences.All().Where(predicate);
我需要这种分离,以便我可以将我的谓词传递到我的存储库并在其他地方使用它。
【问题讨论】: