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

2/10/2010 9:27:17 PM #

I am very glad to read this blog,because very efficient technology developed by Microsoft.Using this you can save our by time writing unspectacular lines of iterations, loops, dump SQL code and Database Adapters.Read and take information about it,Thanks.  

Cellulite Treatment United States | Reply

2/11/2010 4:13:09 PM #

Brilliant post mate, keep up the good work

attract girls United States | Reply

2/11/2010 7:45:51 PM #

I'm pretty much sure that there are so much to learn from this site, because very efficient technology developed by Microsoft.Using this you can save our by time writing unspectacular lines of iterations, loops, dump SQL code and Database Adapters.Read and take information about it,
Thanks.  

Cellulite Cream United States | Reply

2/12/2010 8:17:30 PM #

I’m impressed, you know what you’re talking about

buy laptop United States | Reply

2/12/2010 9:46:19 PM #

very useful info. Hope to see more posts soon!

learn poker United States | Reply

2/12/2010 9:46:37 PM #

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

Employment and Training United States | Reply

2/15/2010 4:50:16 PM #

very useful info. Hope to see more posts soon!

best home based business United States | Reply

2/15/2010 5:28:03 PM #

Very useful info. Hope to see more posts soon!

Internet dating United Kingdom | Reply

2/17/2010 9:02:42 PM #

great post.like your blog.

designer rugs United States | Reply

2/22/2010 7:17:57 PM #

Hi,
  Great work buddy, keep it up

childrens tricycle United States | Reply

2/22/2010 11:25:05 PM #

A very nice n well designed game which is suitable to play for both youngsters as well as elders.

live cricket scores United States | Reply

2/23/2010 4:52:51 AM #

Vielen Dank für die nützlichen Informationen! Ich handy!

online pokern Germany | Reply

2/23/2010 5:06:37 PM #

Really appreciate this post. It’s hard to sort the good from the bad sometimes, but I think you’ve nailed it!

cheap printer ink United States | Reply

2/24/2010 4:16:43 PM #

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

Income protection insurance United States | Reply

2/24/2010 4:17:17 PM #

Appreciate the info, it’s good to know.

management mentoring United States | Reply

2/24/2010 7:12:15 PM #

Cheers to the author for giving me some solid ideas....

http://www.quantumfinancesolutions.com.au/

car loans sydney United States | Reply

2/25/2010 9:30:33 PM #

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


bamboo floors United States | Reply

2/25/2010 10:06:30 PM #

Hi,


Nice job, it’s a great post. The info is good to know!

Zone Diet   United States | Reply

2/26/2010 4:51:25 PM #

I like your blog post, keep on writing this great material!  I'll make sure to follow up on your blog in the future.

Accounting Software United States | Reply

2/26/2010 4:52:01 PM #

I would like to share it with all my friends and hope they will like it too.

Web Design New Jersey United States | Reply

2/26/2010 4:52:29 PM #

Usually I do not post on blogs, but I would like to say that this blog really forced me to do so! Thanks, really nice post.

Web Design India United States | Reply

2/26/2010 4:52:59 PM #

Glad to visit your blog. Thanks for great post that you share to us...

Zoekmachine optimalisatie United States | Reply

2/26/2010 4:53:30 PM #

Wow, what a great resource! Thanks for sharing this...

SEO Virginia United States | Reply

2/26/2010 4:54:04 PM #

Thanks for sharing info. Keep up the good work...We hope you will visit our blog often as we discuss topics of interest to you

Top SEO Companies India United States | Reply

2/27/2010 7:10:19 AM #

I enjoyed your website

best wedding songs United States | Reply

3/1/2010 3:48:25 PM #

A good read, definitely worth a cut and paste. Thanks!
http://www.unitylink.com.au/

administration job United States | Reply

3/1/2010 5:41:57 PM #

hi......
Awesome post! Interesting info to know.

darwin fishing charter United States | Reply

3/3/2010 4:41:43 PM #

Hi,


Nice job, it’s a great post. The info is good to know!

compare odds  United States | Reply

3/4/2010 4:23:46 PM #

Hi,


How do you deduct proffessional tax from your Gross salary in ITR 2 ? In which Schedule?

japan United States | Reply

3/4/2010 8:00:17 PM #

Great work buddy, keep it up

it jobs United States | Reply

3/5/2010 11:23:34 AM #

Yeah, coding is now easy as hell!
Thanks for this MS :o

cellulite removal | Reply

3/5/2010 4:36:57 PM #

hi......
   Appreciate the info, it’s good to know.

hot water solar panel United States | Reply

3/5/2010 4:38:05 PM #

very interesting and very helpful
I will try with your guidance of this article
and of course I will visit your blog every day

batu permata Indonesia | Reply

3/7/2010 2:43:07 AM #

Very informative and interesting, love this stuff, thanks.

3 wheeler buggy United States | Reply

3/8/2010 12:10:22 PM #

I really don't have the enough knowledge to comments in articles on web design. I'm searching for articles about beds and dropped into this. Anyway, thanks for this post.

bedroom furniture beds United States | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




Please type "22" 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