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