15

I'm trying to use the PredicateBuilder, as described here - http://www.albahari.com/nutshell/predicatebuilder.aspx

The following code

var predicate = PredicateBuilder.False<StreetDTO>();

        predicate = predicate.Or(p => p.Locality.Contains(criteria.Locality));
        predicate = predicate.Or(p => p.Name.Contains(criteria.Name));
        predicate = predicate.Or(p => p.Town.Contains(criteria.Town));

        List<StreetDTO> streetData = StreetData.Instance();

        var streetList = from street in streetData.Where(predicate)
                         select street;

as far as I see this should work, according to the example

var newKids  = Product.ContainsInDescription ("BlackBerry", "iPhone");

var classics = Product.ContainsInDescription ("Nokia", "Ericsson")
                      .And (Product.IsSelling());
var query =
  from p in Data.Products.Where (newKids.Or (classics))
  select p;

but all I get is

Error 1 The type arguments for method 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I'm trying to gain some understanding in LINQ 'on-the-job', so apologies if this is a simple question.

crthompson
  • 15,653
  • 6
  • 58
  • 80
Duncan
  • 10,218
  • 14
  • 64
  • 96

2 Answers2

18

Ah; your list is using IEnumerable<T> extension methods (rather than IQueryable<T>) - try:

var streetList = from street in streetData.AsQueryable().Where(predicate)
                 select street;
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
11

Try compiling your predicate:

var streetList = from street in streetData.Where(predicate.Compile())
                 select street;
bruno conde
  • 47,767
  • 15
  • 98
  • 117
  • Thanks, this also worked, although I marked Marc as the answer as I found another answer that matched his elsewhere. I'm not sure which way is preferred? – Duncan May 18 '09 at 10:36
  • @Duncan, I'm not sure witch is the preferred way either :( This has to do with how linq works it's magic *inside*. I don't believe that you'll hit any major problems in terms of performance if you use one or the other. – bruno conde May 18 '09 at 11:07