LINQ - Passing query results

by aristosamar 22. December 2007 06:11

There is something in C# I am crazy about last time. There really is.

It is the LINQ, which stands for Language INtegrated Query.LINQ came up along with C# 3.0 and .Net Framework 3.5.I really enjoy it...

but...

Have a look at this code:

 

It works ok unless we want to pass the result out from the Fo method by some way.

Using var query this trick is not possible, because of an unknown type (maybe rather dynamic type) the var will converted into on the compile time.

It is not possible to pass var query into a method too because of the same reason.It looks to me like the biggest disadvantage of LINQ.The fact that we are unable to pass var into and pass it out of the method means that everything we want to do with the query result has to be done within the Fo() method.

It is not very convenient I would say.
I would rather think about something like this:

 

This would be nice but it wont work. The type of var query is dynamically created by compiler which takes a look at the syntax on the right side of = sign.
Trying to declare var query as we have done in the last example, will give no chance for compiler to figure out the type of the var.

I was looking for the solution and I have found it somewhere on the net.So, to be able to pass the result whether out or into the method, or even declare our variable outside the method and use it within it we can use IEnumerable , which is in System.Collections.Generic namespace.

Lets try something like that:

 

Right now your result is accessible from any part of your class.
It can be also passed into and out of the method.

It made me happy about LINQ again...

Tags: ,

C#

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.0.0
Theme by Mads Kristensen

About the author

I give up things to be able to code and create virtual worlds... because I have a passion... I don't want to let it go... and I am greateful for it...

Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's or anyone view in  anyway.

© Copyright (C) 2008 Mariusz Zaleski