5 Replies - 502 Views - Last Post: 20 February 2011 - 02:40 PM Rate Topic: -----

#1 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Why does this LINQ code output this?

Posted 20 February 2011 - 12:34 PM

I'm reading through "Visual C# 2008" and I've finally got to the chapter about LINQ.
I've been following it quite well, but the author has introduced the idea of using multiple from clauses and doesn't explain how the code gets the output.

The code is
var groupA = new[] { 3, 4, 5, 6 };
            var groupB = new[] { 6, 7, 8, 9 };

            var someInts = from a in groupA // Required first from clause.
                           from b in groupB // First clause of query body.
                           where a > 4 && b <= 8
                           // Object of anonymous type.
                           select new { a, b, sum = a + b };    

            foreach (var a in someInts)
            {
                Console.WriteLine(a);
            }



This outputs:
{ a = 5, b = 6, sum = 11 }
{ a = 5, b = 7, sum = 12 }
{ a = 5, b = 8, sum = 13 }
{ a = 6, b = 6, sum = 12 }
{ a = 6, b = 7, sum = 13 }
{ a = 6, b = 8, sum = 14 }

I have no idea how this outputs the above, I understood the previous examples that used one from clause.
But this has me confused :S
Could someone give me a quick explanation of what's going on here? I don't want to read on without understanding :)

Thank you for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Why does this LINQ code output this?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Why does this LINQ code output this?

Posted 20 February 2011 - 12:48 PM

Think of this like a nested loop. For each of the items in group A, we are going to get the items from group B. We are then selecting only those elements where A is greater than 4 and B is less than or equal to 8. Convert the two froms into loops. :)

So from the first group we are selecting each of those elements from the second group. This is going to give us all permutations of the two groups. When A is equal to 3 we go through each of group B. When A is then change to 4, we go through the second group again and so on.

:)
Was This Post Helpful? 1
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Why does this LINQ code output this?

Posted 20 February 2011 - 12:56 PM

Hi,

Think of it like this. The first from clause goes through each of the elements in the groupA array, correct? The second from clause is like a nested loop. So we are saying foreach element in group A, do this foreach element in groupB. For example,

the first time through, it 'selects' the 3 from groupA. It then (by virtue of the second from clause) goes through EVERY element in groupB, checking the condition as it goes. It does this foreach element in groupA.

Here is a, in principle, functionally equivalent example. Maybe it'll help you see what's going on a bit better:

foreach (int a in groupA) //first from clause
{
     foreach (int b in groupB) //second from clause
     {
         //where condition and select statement (obviously I don't 'select' an element in the same way here, I just print each out as they come :)/>
         if (a > 4 && b <= 8) Console.WriteLine("{0}, {1}, {2}", a, b, a + B)/>; <--- should be lower case 'b'...
     }
}



EDIT: Note in the 'a + B' part, the 'B' should be lower case. It won't let me put a lower case 'b' for some reason :)

This post has been edited by CodingSup3rnatur@l-360: 20 February 2011 - 01:38 PM

Was This Post Helpful? 1
  • +
  • -

#4 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Why does this LINQ code output this?

Posted 20 February 2011 - 02:12 PM

Brilliant :)

Thanks guys, quite the head screw but I think I get it now :D
Was This Post Helpful? 0
  • +
  • -

#5 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Why does this LINQ code output this?

Posted 20 February 2011 - 02:20 PM

Actually there's one thing I don't understand, why does the console.writeline() only have 'a' in it and not 'b' as well?
*edit* I just realised I was confused because the author uses a as the foreach iterator :whistling:

This post has been edited by DivideByZero: 20 February 2011 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Why does this LINQ code output this?

Posted 20 February 2011 - 02:40 PM

Yeah, that is actually quite misleading. In that foreach loop, 'a' isn't a number, it's an anonymous type that holds the numbers 'a', 'b', and 'a' + 'b'.

The query effectively returns an IEnumerable of objects of an anonymous type. That foreach loop is looping through each object in that IEnumerable collection. Therefore, 'a' is an object of an anonymous type selected by the query; it has nothing to do with the variables individual numbers 'a' and 'b' in the linq query itself :).

This post has been edited by CodingSup3rnatur@l-360: 20 February 2011 - 03:23 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1