How to call a L2S provider to translate a predicate in form of Func<T,bool> to a SQL where clause?

dapper linq-to-sql predicate sql sql-server-2008

Question

I am using L2S in my application but i'm switching more and more to dapper.net because i'm having major issues with query performance and the annoying n+1 selects problem.

Its working fine, but i miss the comfortable LINQish style of writing predicates when filtering data.

So my question is, how can i translate a predicate in form of Func<T, bool> to a SQL Server where clause to use it with dapper?

I think someone must have done this before, or are all the dapper users handcoding their sql statements?

As the title suggests, maybe it is an option to call the LINQ2SQL provider for SQL Server?

Basically i'm looking for something like the inverse of dynamic linq.

Popular Answer

There are ways to see the SQL generated by LINQ. See the article Seeing the SQL Generated by LINQ to Entity Queries from Visual Studio Magazine.

Now that I understand better what you're after, I did a little more looking into this and found a couple of related posts that are worth a look:

Dynamic where clause in dapper which suggests using a Stringbuilder, but one of the comments points to a Sam Saffron article, Porting LINQ-2-SQL to Dapper for great justice, that talks about a contributed SqlBuilder that might help you.

Generate a SQL clause using a Linq-to-Sql Expression which suggests using a LINQ Where call and grabbing the WHERE clause from the generated SQL.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why