开发者

convert linq to object query to sql query (no linq to sql code or datacontext)

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 ??


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜