1

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.

Community
  • 1
  • 1
Jan
  • 15,802
  • 5
  • 35
  • 59

1 Answers1

2

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.

Community
  • 1
  • 1
JamieSee
  • 12,696
  • 2
  • 31
  • 47
  • I know, that i can see the SQL generated by L2S. But thats not the point. I'm looking for code to generate that kind of SQL. – Jan Dec 23 '11 at 21:21
  • @Jan Edited my answer to, hopefully, be more helpful. Let me know if it still doesn't satisfy. – JamieSee Dec 24 '11 at 00:09
  • Ok, thats more helpful - especially the link to sams blog entry about the SqlBuilder. Thats not exactly what i'm looking for, but it goes in the right direction. – Jan Dec 24 '11 at 22:08