2015-09-26 54 views
1

我想獲得工作發現在http://www.albahari.com/nutshell/predicatebuilder.aspxLinqPad Predicatebuider表達通話不能正常工作

的例子,我有以下代碼:

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(間)方法不起作用代碼的功能是相同的,但是當我調用Method 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' 

回答

0

嘗試

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

PS,你舉的例子不能編譯,所以我猜你的意思

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

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