0

I am trying to create an expression tree which will look through my table called test for all strings 'email' in column Foo. I pretty much copied this from the msdn with some small changes and cannot get it to search through my table. Any help getting this expression tree to search would be great. THANK YOU very much

Table Structure called Test

id int
foo char(10)

Error

No generic method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied type arguments

var s = new m2Entities().Test
var queryableData = new m2Entities().SaleLogs.AsQueryable<SaleLog>();
ParameterExpression pe = Expression.Parameter(typeof(string), "foo");
Expression left = Expression.Call(pe, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
Expression right = Expression.Constant("bar");
Expression e1 = Expression.Equal(left, right);

MethodCallExpression whereCallExpression = Expression.Call(typeof(Queryable),
                                                           "Where",
                                                           new Type[] { queryableData.ElementType },
                                                           queryableData.Expression,
                                                           Expression.Lambda<Func<string, bool >>(e1,new ParameterExpression[] { pe }));
var results = queryableData.Provider.CreateQuery<string>(whereCallExpression);
svick
  • 236,525
  • 50
  • 385
  • 514
gh9
  • 10,169
  • 10
  • 63
  • 96
  • 1
    Have you tried to write the same expression by hand? How would that look like? Hint: you can't call `string.ToLower()` on something that's not actually a `string`. – svick Jan 07 '13 at 22:45
  • @svick using a normal linq query m2Entities().Test.FirstOrDefault(x => x.Foo =='bar'); ??? The normal linq query works just fine – gh9 Jan 07 '13 at 22:46
  • But there is no `x` or `.Foo` in the query you're building. It's actually something like `foo => foo.ToLower() == "bar"`. – svick Jan 07 '13 at 22:50
  • I do not understand expression trees fully yet, but the msdn page doesn't indicate that i need the 'x', I do have the 'foo' in line 3 to match what column i want to search on. – gh9 Jan 07 '13 at 22:54
  • http://stackoverflow.com/questions/1246576/dynamic-where-for-listt/1246619#1246619 Is the answer that most helped me, incase any else needs this. – gh9 Jan 08 '13 at 22:17

1 Answers1

1

You try to call Where like this:

Queryable.Where<SaleLog>((string pe) => pe.ToLower() == "bar")

That is not right. It should be:

Queryable.Where<SaleLog, bool>((SaleLog pe) => pe.foo.ToLower() == "bar")

The error message is hinting at this:

No generic method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied type arguments

It is entirely accurate.

usr
  • 168,620
  • 35
  • 240
  • 369