Reputation: 1330
How can i convert linq to object query or any other Func delegate to string like sql statements
for example
var cat_list = new List<Cat> { ... };
var myquery = cat_list.Where(x => x.Age > 2 && x.Name.Contains("Kitty"));
Now myquery is IEnumerable<Cat>
. how can i convert this to simply something like this
"Age > @p1 AND Name LIKE @p2"
how can i achieve this ??
Upvotes: 2
Views: 1325
Reputation: 419
Check out the method DataContext.GetCommand() which is passed an IQueryable object and returns the DbCommand object that corresponds to the query. The CommandText property of the DbCommand object shows the text of the query.
Upvotes: 0
Reputation: 61467
You could write an expression tree parser and generate the sql. Your description contains a fault - myquery
isn't IQueryable<Cat>
, it is an IEnumerable<Cat>
. As you tagged it correctly, this is linq-to-objects, not linq-to-sql. There is no information in the calls to construct a query.
Upvotes: 1
Reputation: 244928
Doing something like that is not simple. Have a look at the series of articles Building an IQueryable provider by Matt Warren. All the code he uses is available as a library too. That should help you get started.
Upvotes: 2