tags:

views:

41

answers:

3

DynamicObject LINQ query with the List compiles fine:

List<string> list = new List<string>();
var query = (from dynamic d in list where d.FirstName == "John" select d);

With our own custom class that we use for the "usual" LINQ compiler reports the error "An expression tree may not contain a dynamic operation":

DBclass db = new DBclass(); var query = (from dynamic d in db where d.FirstName == "John" select d);

What shall we add to handle DynamicObject LINQ?

A: 

You could add a type, against which to write the query.

David B
+1  A: 

Does DBClass implement IEnumerable? Perhaps there is a method on it you should be calling to return an IEnumerable collection?

ThatSteveGuy
A: 

I believe your problem is, that in the first expression, where you are using the List<>, everything is done in memory using IEnumerable & Link-to-Objects.

Apparently, your DBClass is an IQueryable using Linq-to-SQL. IQueryables use an expression tree to build an SQL statement to send to the database.

In other words, despite looking much alike, the two statements are doing radically different things, one of which is allowed & one which isn't. (Much in the way var y = x * 5; will either succeed or fail depending on if x is an int or a string).

Further, your first example may compile, but as far as I can tell, it will fail when you run it. That's not a particular good benchmark for success.

The only way I see this working is if the query using dynamic is made on IEnumerables using Link-to-Objects. (Load the full table into a List, and then query on the list)

James Curran