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);
5
  • 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. Commented Jan 7, 2013 at 22:45
  • @svick using a normal linq query m2Entities().Test.FirstOrDefault(x => x.Foo =='bar'); ??? The normal linq query works just fine Commented Jan 7, 2013 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". Commented Jan 7, 2013 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. Commented Jan 7, 2013 at 22:54
  • stackoverflow.com/questions/1246576/dynamic-where-for-listt/… Is the answer that most helped me, incase any else needs this. Commented Jan 8, 2013 at 22:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.