1
我有以下功能如何结合2 Linq的谓词〜C#
public virtual ICollection<T> initData<T>(System.Data.Entity.DbSet<T> set, System.Linq.Expressions.Expression<Func<T, bool>> filter) where T : CModel<T>
{
var x = (from dc in set select dc);
if (!this.db.valid)
{
System.Linq.Expressions.Expression<Func<T, bool>> active = a => a.active;
filter = (Expression<Func<T, bool>>)Expression.Lambda(Expression.AndAlso(filter, active));
x.Where(filter);
}
else
{
x.Where(filter);
}
return (ICollection<T>)x.ToList();
}
当过我尝试了2个谓词与AndAlso
结合我抛出一个异常:
The binary operator AndAlso is not defined for the types 'System.Func`2[namespace.Models.MyClass,System.Boolean]' and 'System.Func`2[namespace.Models.MyClass,System.Boolean]'.
我怎么能结合这两个条件?
您可以使用谓词构建器。 http://www.albahari.com/nutshell/predicatebuilder.aspx – Flowerking 2013-02-10 10:57:04
[LINQ to Entities:Combining Predicates](http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-到实体,合并,predicates.aspx) – 2014-06-17 05:38:16