Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Linq queries from string in C#4.0 - best practice

Currently I am using LinqKit / Ms dynamic query example to dynamically build Linq Expressions from strings. This works fine.

  1. LinqKit: http://www.albahari.com/nutshell/linqkit.aspx
  2. Microsoft dynamic Linq queries: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Right now, I am migrating my application from C#3.5 to C#4.0. I am wondering if there is another way (standard way of the framework) to build queries from strings.

I have checked the documentation, but did not find anything yet. Also this is not an issue, since I have the above solution.

Only I'd prefer to use the "standard" features if there some. What's the best practice?

like image 456
Horst Walter Avatar asked Jan 08 '11 16:01

Horst Walter


1 Answers

I'm currently doing something like this and I'm very happy with the result. The way I did it was with Entity Framework and the ObjectQuery.Select(string query, ObjectParameters[] params) method. More info here: http://msdn.microsoft.com/en-us/library/bb298787.aspx#Y586.

You won't be making expression from string but using SQL to Entities which does the work very well and was made exactly for that purpose as dynamically making Expression isn't trivial and is actually slower.

Cheers

like image 66
Fabian Nicollier Avatar answered Sep 22 '22 23:09

Fabian Nicollier