LinqPad Predicatebuider Expression调用不起作用

时间:2015-09-26 06:30:51

标签: linqpad predicatebuilder linqkit

我正在尝试在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'

1 个答案:

答案 0 :(得分:0)

尝试

   plList.AsQueryable().Where(PriceList.IsCurrent()).Dump();  

PS,你的例子没有编译,所以我猜你的意思是

public static Expression<Func<PriceList, bool>> IsCurrent()
{
...

List<PriceList> plList = new List<PriceList>()
...