1

I am trying to use predicate builder in the following code:

public ListResults<DBAccountDetail> GetAccountDetail(string[] salesForceKey)
    {
        try
        {
            using (var c = new SalesForceDataContext())
            {
                var predicate = PredicateBuilder.False<DBAccountDetail>();

                foreach (var keyword in salesForceKey)
                {
                    var temp = keyword;
                    predicate = predicate.Or(p => p.Id.Contains(temp));
                }

                var lret = c.DBAccountDetails.AsQueryable().Where(predicate).ToList();
                return new ListResults<DBAccountDetail>(lret);
            }
        }
        catch (Exception ex)
        {
            LogError("GetLegacyRateLetters()", ex);
            return new ListResults<DBAccountDetail>(ex);
        }
    }

The problem is that on this line:

predicate = predicate.Or(p => p.Id.Contains(temp));

p.Id will not intellisense out and throws a compilation error of:

No overload for method 'Or' takes 1 arguments

What is PredicateBuilder

George Mauer
  • 117,483
  • 131
  • 382
  • 612

3 Answers3

1

The fact that you're not getting Intellisense on p.Id tells me that DBAccountDetail.Id probably either doesn't exist, doesn't have a getter, or is private. If you aren't getting intellisense on the "p", then maybe the compiler isn't resolving DBAccountDetail correctly? Without more information, it's not clear what the problem may be.

However, it is probably worthwhile to note that the latest versions of Entity Framework and LINQ to SQL both support syntax like this:

c.DBAccountDetails.Where(d => salesForceKey.Contains(d))

... and this:

c.DBAccountDetails.Where(d => salesForceKey.Any(k => k == d))

Either of these would make PredicateBuilder unnecessary in this case.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0

I have the same issue but I had placed the PredicateBuilder class in another project. Once I moved it into the same project as my Linq To Sql classes the error went away.

0

It is possible that the compiler cannot guess the generic type for Or. Try providing it directly

predicate = predicate.Or<DBAccountDetail>(p => p.Id.Contains(temp))

As an aside, you should be able to 1-line that foreach with

var predicate =
salesForceKey.Aggregate(
  PredicateBuilder.False<DBAccountDetail>(),
  (accumulatedPredicate, keyword) => accumulatedPredicate.OrDBAccountDetail>(p => p.Id.Contains(temp))
);
George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • On the above code, I get the following error:Error 1 The non-generic method 'System.Linq.Expressions.Expression.Or(System.Linq.Expressions.Expression, System.Linq.Expressions.Expression)' cannot be used with type arguments – Andrew Lambrecht Jan 14 '11 at 14:13