IUnknown
Windows Azure mumblings of IUnknown

Thou Shalt not Iterate! : Part 1

Thursday, February 25, 2010 4:56 PM

LINQ as we all know provides intrinsic data querying capabilities to the framework languages supporting LINQ. Going forward its an obvious way to code clean, and with growing number of LINQ providers (LINQ to SQL, LINQ to Entities, LINQ to XML, LINQ for Maps, and not to forget DryadLinq and pLinq for us Cloudy folks’ Parallel Computing needs, and so on) it looks to be the de facto standard for querying within dotnet framework languages. 

Let us get acquainted to LINQ by taking a close look at the beauty and simplicity of LINQ, instead of getting into technical nitty-gritty’s. 

We can start off with simple scenarios where we inherently use loops and replace the codes with LINQ equivalents. Going forward we can include complex looping operations in growing order of complexities. 

To kick off let’s look at some interesting Linq alternatives to de-rigueur C# code:

 

 //Initialize an array with 10 as default value 

 //Loop
int[] iArr = new int[11];
for(int i = 0; i < 11; i++)
{
   iArr[i] = 10;
}

 //LINQ
int[] iArr = Enumerable.Repeat(10, 11).ToArray();  

//Fill an array with 1 to 100

//Loop
int[] iArr = new int[100];
for(int i = 1; i < 101; i++)
{
   iArr[i-1] = i;
}

//LINQ
int[] iArr = Enumerable.Range(0, 100).ToArray();

 

//Fill an array with 100, 150, 200, 250, ..... upto 100 Terms

//Loop
int[] iArr = new int[100];
for (int i = 0; i < 100; i++)
{
    if (i != 0)
    {
         iArr[i] = iArr[i-1]+50;
    }
    else
    { 
        iArr[i] = 100;
    }
}

 //LINQ
int[] c = Enumerable.Range(0, 100).Select(i => 100 + 50 * i).ToArray();

Feel free to provide valuable feedback about the kind of content you expect in this initiative.

PS: Thanks to Nathan Kelly, this post stands corrected.



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Thou Shalt not Iterate! : Part 1

How nice is that?

int niceness = Enumerable.Range(0, 10).Max();

Great! 2/25/2010 8:33 PM | Cédric Menzi

# re: Thou Shalt not Iterate! : Part 1

you are using Enumerable.Repeat on your first linq example and your equivalent iterative loop above seems to indicate you meant to use Enumerable.Range

and you should declare your second example's array as new int[101] 2/25/2010 8:35 PM | Nathan Kelley

# re: Thou Shalt not Iterate! : Part 1

but otherwise linq rocks. declarative approaches are so much more readable and maintainable than long nested for loops.

Of course, in my experience, people fight so hard against a "better way" and so I expect most developers to stick with their comfortable iterative loops. 2/25/2010 8:39 PM | Nathan Kelley

# re: Thou Shalt not Iterate! : Part 1

Thank you, Nathan! Corrected the content. Enthusiasm is a funny thing oftentimes.

PS: I remember having circulated your WTFs/Minute post in the bay :D. That was a nice article. 2/25/2010 9:51 PM | Sarang

# re: Thou Shalt not Iterate! : Part 1

I'm on the next to last day of my 2 week notice here and I am starting a new job next Monday. That post has just become 100% relevent. My current company is interviewing candidates and I bet I know 5 things that won't be asked of any candidates : Source Control, Unit Testing, Dependency Injection, Automated Builds, and Interfaces.

I offer him/her my condolences while they utter countless WTFs.

To keep this reply relevent :) I moved to .Net 3.5 as soon as I could so that I could start using Linq and Lambdas. Self improvement is important to me and it didn't take much to see the benefit of clearer code in fewer lines. I wonder if the guy that replaces me will know Linq.

On a side note the new gig will be C#. Thank you {diety} no more VB. 2/26/2010 2:27 AM | Nathan Kelley

Post a comment