The official Fatica Labs Blog! RSS 2.0
# Thursday, 17 November 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”)
              .Skip(first)
              .Take(count)
              .ToList();

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, 17 November 2011 13:15:19 (GMT Standard Time, UTC+00:00)  #    Comments [5] - Trackback
CodeProject | NHibernate

Thursday, 17 November 2011 22:50:08 (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, 18 November 2011 06:19:46 (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, 21 November 2011 02:42:37 (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, 21 November 2011 08:18:36 (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, 20 February 2012 07:37:43 (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.
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(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
Contacts

Send mail to the author(s) E-mail

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

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
Statistics
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