Dynamic LinQ is love

by Daniel 11. June 2008 10:54

Microsoft's LinQ - in any shape - is in my opinion one of the most impressive and useful technology stacks that Microsoft has released since .NET itself. I loved it from the very first day and it has saved me ours of time writing unspectacular lines of iterations, loops, dump SQL code and Database Adapters. Since Microsoft implemented LinQ as a language feature it is of course type safe and in many szenarios very good this way. But when it becomes a little bit more dynamic, the initially strongly typed nature of a LinQ statement is a little bit of a barrier. Sometimes I need really dynamic queries that are a result of several unpredictable parameters defined at runtime. That's where "dynamic link" comes into play. Scott blogged about this "LINQ Dynamic Query Library" at the beginning of this year. Very nice!

But to be honest - after using it several times - it seems to me like a forgotten feature that only few care about. Besides the fact that it's basically just a simple class that triggers some runtime compilation - it's quite poorly documented and slightly uncomplete. Since it's untyped, accessing the resulting data can become quite unhandy. I wrote some additional Extension Methods that I initially missed and now I use them quite frequently and I'd like to share with you.

The first extension method I was really looking for was the "distinct" feature of LinQ. It's not that special - but when you are in a hurry and you have no experience in writing these extension methods it may become quite useful:

/// <summary>
/// Distincts the specified source.
/// </summary>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IQueryable Distinct( this IQueryable source )
{
  
if( source == null )throw new ArgumentNullException( "source" ); 
   return source.Provider.CreateQuery(
     
Expression.Call( typeof( Queryable ), "Distinct", new Type[] { source.ElementType }, source.Expression ) );
}

 

The next extension method I added is one to provide the resulting data as a simple typed List. It makes sense only for one dimensional result sets of course:

/// <summary>
/// Converts the first column of the specified source into a List of type T.
/// </summary>
/// <param name="source">The source.</param>
/// <returns></returns>
public static List<T> ToList<T>( this IQueryable source )
{
  
List<T> r_List = new List<T>();
  
PropertyInfo l_FirstProperty = null;
  
IEnumerator l_Enumerator = source.GetEnumerator();
  
while( l_Enumerator.MoveNext() )
   {
     
object l_CurrentObject = l_Enumerator.Current;
     
if( l_FirstProperty == null )
      {
        
PropertyInfo[] l_PropertyInfoCollection = l_CurrentObject.GetType().GetProperties();
         l_FirstProperty = l_PropertyInfoCollection[0];
      }
      l_List.Add( (T) l_FirstProperty.GetValue( l_CurrentObject,
null ) );
   }
  
return r_List;
}

Currently I am writing Extension Methods that can return result sets as a DataTable or multi dimensional list from a dynamic LinQ query. If you are interested in them, don't hesitate to contact me...

Tags:

Comments

11/27/2009 2:14:10 PM #

I don't have a bunch to to say in reply, I only wanted to sign up to reply well done. I actually got a kick out of your article. It appears that you've placed a good amount of effort into your post and I need a lot more of these on the Internet these days.

Harris County Background Check | Reply

12/4/2009 3:57:59 AM #

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon

Cleaning services Ottawa United States | Reply

1/11/2010 9:25:13 PM #

Hi
Nice blog thanks for shairing with me. If you want more information about electronics appliance you can visit us
http://www.spyshelter.com

anti keylogger United States | Reply

1/12/2010 3:24:31 PM #

This new application makes coding so easy. System programmers will surely like this.

essay writing service United States | Reply

1/14/2010 3:23:03 AM #

The fast growing internet presents the growing opportunity for men and women, even teenagers, to make a comfortable income from home in their spare time. The steps to achieve financial independence on the net is outlined below. Use them wisely and you could become a very successful web entrepreneur.

discount pool equipment United States | Reply

1/20/2010 10:52:22 AM #

From time to time it falls out that you are lack of time to write your dissertation thesis close to this topic. A lot of people do a big problem of that, just because some of them don’t know a thing about <a href="http://www.primedissertations.com">thesis service</a>, but we will give you suggests about right ways to find the fantastic <a href="http://www.primedissertations.com">buy dissertation</a> service and how to buy a thesis paper with a small strain of your pocket.

Joangc19 Canada | Reply

1/25/2010 7:27:02 PM #

Dynamic LinQ is My love. I don't have a bunch to to say in reply, I only wanted to sign up to reply well done.

nowGoogle.com Adalah Multiple Search Engine PopularnowGoogle.com Adalah Multiple Search Engine Popular United States | Reply

1/27/2010 12:59:37 AM #

I'm pretty much sure that there are so much to learn from this site. So I'll be visiting again to hopefully see new post this 2010Smile

Acoustic Shock United States | Reply

1/27/2010 10:24:08 PM #

I've seen several blog pages from Daniel and I can tell that he has really something to offer hope he'll find time to update all his blog site.

Virtual Office Florida United States | Reply

1/28/2010 9:29:01 PM #

How long have you been in this field? You seem to know a lot more than I do, I’d love to know your sources!

australian health insurance United States | Reply

1/30/2010 5:28:54 PM #

Hi,



It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

japan United States | Reply

1/30/2010 10:59:41 PM #

With these lines "it seems to me like a forgotten feature that only few care about. Besides the fact that it's basically just a simple class that triggers some run time compilation " I absolutely agree with you. Felt the same thing and see it that way too.

Blue Ridge Mountains Cabin Rentals United States | Reply

2/2/2010 8:21:20 PM #

Interesting read, thanks for helping keep me busy at work ..

SEO United States | Reply

2/3/2010 3:57:39 PM #

It's hard to find knowledgeable people on this topic, but you sound like you know what you?re talking about! Thanks ..

Life insurance United States | Reply

2/3/2010 5:12:25 PM #

Top post. I look forward to reading more. Cheers ...

adult sex toy United States | Reply

2/8/2010 5:57:47 PM #

Hi,
  Very useful info. Hope to see more posts soon!

cheap printer ink United States | Reply

2/8/2010 8:02:31 PM #

Useful info. Hope to see more good posts in the future.

escorts United States | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




Please type "60" into the text field:

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Daniel
MCSD, MCTS
Microsoft Certified Trainer
CTO, Albamond GmbH
E-mail me Send mail