Вызов выражения LinqPad Predicatebuider не работает
Я пытаюсь заставить работать примеры, найденные на 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 ответ
Решение
Пытаться
plList.AsQueryable().Where(PriceList.IsCurrent()).Dump();
PS, ваш пример не компилируется, так что я предполагаю, что вы имеете в виду
public static Expression<Func<PriceList, bool>> IsCurrent()
{
...
а также
List<PriceList> plList = new List<PriceList>()
...