7

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?

Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • Here is a related discussion: http://stackoverflow.com/questions/3782538/parsing-a-string-c-linq-expression . I think it's up-to-date for .NET 4, so still nothing built-in into the framework - except if you are talking about Linq to Entities, then EF has dynamic query building capabilities (although not new in .NET 4 as far as I know): http://msdn.microsoft.com/en-us/library/bb338811.aspx – Slauma Jan 09 '11 at 12:28
  • So far, I got my old code working. Minor adjustments are required. E.g., a method call no longer accepts null, but you need to call an overloaded method. Basically simple stuff once you know. However, good to know I obviously did not miss anything. – Horst Walter Jan 10 '11 at 23:27

1 Answers1

2

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

Fabian Nicollier
  • 2,811
  • 1
  • 23
  • 19
  • That's good, took me some time to crosscheck it. However, it is obviously a different approach from "MSDynamicLinq2010" (from the VS2008 examples) and "LinqKit" and hence something new (at least for me). I'll have this in mind for my next solutions. – Horst Walter Mar 24 '11 at 18:12
  • I guess there's more than one way to skin a cat. I remember during a TechEd MS saying that they made Entity SQL with the clear purpose of having an easier alternative to making dynamic Expressions. It's still DB abstract and slightly faster than using expressions so for dynamic stuff I can hardly think of a downside. Works super for me. – Fabian Nicollier Mar 24 '11 at 21:06