LinqPad Predicatebuider 表达式调用不起作用
LinqPad Predicatebuider Expression call not working
我正在尝试让在 http://www.albahari.com/nutshell/predicatebuilder.aspx
中找到的示例正常工作
我有以下代码:
public partial class PriceList
{
public string Name { get; set; }
public string Desc {get;set;}
public DateTime? ValidFrom {get;set;}
public DateTime? ValidTo{get;set;}
public static Expression> IsCurrent()
{
return p => (p.ValidFrom == null || p.ValidFrom <= DateTime.Now) && (p.ValidTo == null || p.ValidTo >= DateTime.Now);
}
}
void Main()
{
List plList = new List()
{
new PriceList(){ Name="Billy", Desc="Skilled", ValidFrom = DateTime.Now.AddDays(-10.0) , ValidTo= DateTime.Now.AddDays(1.0) },
new PriceList(){ Name="Jamie", Desc="Quick Study",ValidFrom =DateTime.Now.AddDays(-10.0) , ValidTo= DateTime.Now.AddDays(1.0) },
new PriceList(){Name= "Larry", Desc= "Rookie",ValidFrom = DateTime.Now.AddDays(-10.0) , ValidTo= DateTime.Now.AddDays(1.0) }
};
// Method 1
var myResultList = plList.Where ( p => (p.ValidFrom == null || p.ValidFrom <= DateTime.Now)
&& (p.ValidTo == null || p.ValidTo >= DateTime.Now)).Dump();
// Method 2
plList.Where(PriceList.IsCurrent()).Dump(); // This does not work currently
}
我的问题是在方法 1 和方法 2 之间,这个 IsCurrent() 方法不起作用代码的功能是相同的,但是当我调用方法 2 PriceList.IsCurrent() 时,我得到以下错误.
如有任何建议,我们将不胜感激。
'System.Collections.Generic.List' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' has some invalid arguments
Instance argument: cannot convert from 'System.Collections.Generic.List' to 'System.Linq.IQueryable'
尝试
plList.AsQueryable().Where(PriceList.IsCurrent()).Dump();
PS,你的例子没有编译,所以我猜你的意思是
public static Expression<Func<PriceList, bool>> IsCurrent()
{
...
和
List<PriceList> plList = new List<PriceList>()
...
我正在尝试让在 http://www.albahari.com/nutshell/predicatebuilder.aspx
中找到的示例正常工作我有以下代码:
public partial class PriceList
{
public string Name { get; set; }
public string Desc {get;set;}
public DateTime? ValidFrom {get;set;}
public DateTime? ValidTo{get;set;}
public static Expression> IsCurrent()
{
return p => (p.ValidFrom == null || p.ValidFrom <= DateTime.Now) && (p.ValidTo == null || p.ValidTo >= DateTime.Now);
}
}
void Main()
{
List plList = new List()
{
new PriceList(){ Name="Billy", Desc="Skilled", ValidFrom = DateTime.Now.AddDays(-10.0) , ValidTo= DateTime.Now.AddDays(1.0) },
new PriceList(){ Name="Jamie", Desc="Quick Study",ValidFrom =DateTime.Now.AddDays(-10.0) , ValidTo= DateTime.Now.AddDays(1.0) },
new PriceList(){Name= "Larry", Desc= "Rookie",ValidFrom = DateTime.Now.AddDays(-10.0) , ValidTo= DateTime.Now.AddDays(1.0) }
};
// Method 1
var myResultList = plList.Where ( p => (p.ValidFrom == null || p.ValidFrom <= DateTime.Now)
&& (p.ValidTo == null || p.ValidTo >= DateTime.Now)).Dump();
// Method 2
plList.Where(PriceList.IsCurrent()).Dump(); // This does not work currently
}
我的问题是在方法 1 和方法 2 之间,这个 IsCurrent() 方法不起作用代码的功能是相同的,但是当我调用方法 2 PriceList.IsCurrent() 时,我得到以下错误.
如有任何建议,我们将不胜感激。
'System.Collections.Generic.List' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' has some invalid arguments
Instance argument: cannot convert from 'System.Collections.Generic.List' to 'System.Linq.IQueryable'
尝试
plList.AsQueryable().Where(PriceList.IsCurrent()).Dump();
PS,你的例子没有编译,所以我猜你的意思是
public static Expression<Func<PriceList, bool>> IsCurrent()
{
...
和
List<PriceList> plList = new List<PriceList>()
...