The official Fatica Labs Blog! RSS 2.0
# Thursday, November 17, 2011

Even if Linq To NHibernate provider allow us to write query in a strongly type manner, it is sometimes needed to works with property names literally. For example in a RIA application a service can receive a column as a string containing the name of the property to order by. Since Linq to NHibernate is a standard Linq provider, we can leverage a standard dynamic linq parser. This is achieved by using an old code by MS, known as System.Linq.Dynamic. By following the link you will find a download location that point to an almost just a sample project that eventually contains the file Dynamic.cs that contains some extension method allowing to merge literal parts in a type safe linq query.

Let’see an example:

var elist = session.Query<MyEntity>()
              .OrderBy(“Name descending”)

I supposed we have a property called Name on the class MyEntity. The OrderBy taking a string as a parameter is an extension method provided by Dynamic.cs, and in order to have it working you just need to merge the file dynamic.cs in your project and import System.Linq.Dynamic. Of course you will have extension for Where and for other linq operators too.

Thursday, November 17, 2011 1:15:19 PM (GMT Standard Time, UTC+00:00)  #    Comments [5] - Trackback
CodeProject | NHibernate

Thursday, November 17, 2011 10:50:08 PM (GMT Standard Time, UTC+00:00)
I'm not sure what you're trying to achieve by having magic strings in there... Can you not just do .OrderByDescending(x => x.Name)
Friday, November 18, 2011 6:19:46 AM (GMT Standard Time, UTC+00:00)
@Philip for sure I can, but there is cases in which you just have the "magic" string, for example the property to sort by comes from an remote rest call.
Felice Pollano
Monday, November 21, 2011 2:42:37 AM (GMT Standard Time, UTC+00:00)
Fair enough. Without running the code myself, does using magic strings like that 'Name' exists before executing the query, or does it just take the string as is, and pass it into the generated SQL? I'm wondering if it's prone to SQL Injection.
Monday, November 21, 2011 8:18:36 AM (GMT Standard Time, UTC+00:00)
@Phillip The Dynamic.Linq actually *compiles* the string to regenerate a valid expression, so if a property does not exist or you try to inject something, an error is raised ( missing property or Syntax error respectively). And yes, I've tried to be safe ;)
Felice Pollano
Monday, February 20, 2012 7:37:43 AM (GMT Standard Time, UTC+00:00)
Awesome! This would save lot of code. I will give a try with nhibernate
All comments require the approval of the site owner before being displayed.
Please login with either your OpenID above, or your details below.
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, strike, strong) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
My Stack Overflow

Send mail to the author(s) E-mail

profile for Felice Pollano at Stack Overflow, Q&A for professional and enthusiast programmers
About the author/Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2019
Felice Pollano
Sign In
Total Posts: 157
This Year: 0
This Month: 0
This Week: 0
Comments: 127
This blog visits
All Content © 2019, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano