Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

News

My Friends

My Links

Permanent Posts

Portuguese Communities

Dynamic LINQ

Included with Microsoft's .NET samples, which you can get here lies an hidden gem: a set of method extensions for IQueryable and IQueryable<T> that allow execution of lambda expressions specified as strings, which is handy if you want to generate expressions dynamically.

The actual code is in the CSharpSamples\LinqSamples\DynamicQuery folder. I think Microsoft could have arranged it slightly better, in order to promote quicker reuse, but probably that wasn't on their minds. The extensions are offered by class DynamicQueryable and consist of:

  • GroupBy
  • OrderBy
  • Select
  • Skip
  • Take
  • Where

There are also other useful classes, like DynamicExpression, which parses a String into a lambda.

With these, you can do things like:


Func<SomeClass, Boolean> filterExpression = DynamicExpression.ParseLambda<SomeClass, Boolean>("SomeProperty == true && SomeOtherProperty > 10");  //create lambda from string
var list = dataSource.OrderBy("PropertyName").Take("10").Skip("1").ToList();  //string instead of lambda

For your convenience, I attach a DLL which only contains the reusable parts of the project, in the System.Linq.Dynamic namespace. I have been using this, and I find it very helpful, have a look at it, and happy dynamic LINQing!

Bookmark and Share

Comments

Twitter Trackbacks for Dynamic LINQ - Development With A Dot [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Dynamic LINQ - Development With A Dot         [asp.net]        on Topsy.com

# December 3, 2009 11:12 AM

Chris Eargle said:

Since DynamicQuery builds an expression tree, you can do powerful stuff with this.

# December 4, 2009 11:54 AM

Peter Y said:

Many thanks indeed!  This is exactly the sort of thing I couldn't quite work out how to do neatly, and here it is all nicely wrapped up.  

# December 4, 2009 4:28 PM

Mugun said:

Thanks for the great post.Anyway, i try to create dynamic select statement which doesn't return a result from datatable as expected using your System.Linq.Dynamic.dll

Dim q = From el In dtResult.AsEnumerable.AsQueryable _

          Select ("new(.Result=Column1)")

Is it anything wrong with my statement? I have build original System.Linq.Dynamic from Microsoft samples and it throw an exception "No field or property exist on..."

Thanks

# December 23, 2009 6:13 AM

Ricardo Peres said:

Don't know VB, but I'm not sure if you can use the ".Property" syntax. Also, I thing AsQueryable won't work, you must use the AsQueryable(Of SomeType) version.

# December 28, 2009 8:37 AM

Development With A Dot said:

I wrote a NHibernateDataSource, which can be used in ASP.NET web forms applications just like their counterparts

# January 1, 2010 6:27 PM

NHibernateDataSource | I love .NET! said:

Pingback from  NHibernateDataSource | I love .NET!

# January 1, 2010 9:01 PM

Richard Guo said:

Thanks. I was looking for a easy way to validate a user inputed expression such as (x>0 && y>0 || z>0). From your post I found that I can use the ParseLambda method to do the job.

# January 31, 2010 5:01 AM

Development With A Dot said:

Since .NET 3.5 brought us LINQ and expressions, I became a great fan of these technologies. There are

# April 9, 2010 9:27 AM

Ricardo Peres said:

Desde que .NET 3.5 nos trouxe LINQ e express&otilde;es, eu tornei-me um grande fan destas tecnologias

# April 9, 2010 9:34 AM

Development With A Dot said:

You may recall my post on Dynamic LINQ . I said then that you had to download Microsoft's samples and

# April 9, 2010 10:06 AM

Ricardo Peres said:

Podem lembrar-se do meu post sobre Dynamic LINQ . Eu disse nessa altura que era preciso fazer download

# April 9, 2010 10:12 AM

Community Blogs said:

Continuing my previous posts on dynamic LINQ , now it&#39;s time for dynamic filtering. For now, I&#39;ll

# April 10, 2010 1:31 PM

Ricardo Peres said:

Continuando os meus posts anteriores sobre LINQ Din&acirc;mico , agora &eacute; tempo de falar de filtragem

# April 11, 2010 5:22 AM

Danny Hansen » Linq to SQL generic getPrimaryKey said:

Pingback from  Danny Hansen &raquo; Linq to SQL generic getPrimaryKey

# July 20, 2010 8:42 AM

weblogs.asp.net said:

Dynamic linq.. Huh, really? :)

# April 6, 2011 9:12 AM

weblogs.asp.net said:

Dynamic linq.. Bully :)

# May 1, 2011 11:27 AM

yadiermolina said:

Hi Guys, i was reading below article. I having problem with .Search, it isnt in the .dll. Do you guys now where to find it?

thanks!

naspinski.net/.../Universal-IQueryable-Search-Usable-by-Linq-to-SQL-and-Linq-to-Entities.aspx

# May 21, 2011 5:29 AM

weblogs.asp.net said:

Dynamic linq.. Keen :)

# June 14, 2011 2:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)