Simple C# console showing what happens when using LINQ Select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace LinqProjectionsTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var list = new List<int> { | |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 | |
}; | |
Console.WriteLine("list.Select(...) starting"); | |
var projected = list.Select(num => | |
{ | |
var projection = num + 20; | |
Console.WriteLine("{0} was selected", projection); | |
return new | |
{ | |
Original = num, | |
Projection = projection | |
}; | |
}).Select(num => | |
{ | |
var projection = num.Projection * 2; | |
Console.WriteLine("{0} was selected", projection); | |
return new | |
{ | |
Original = num, | |
Projection = projection | |
}; | |
}); | |
Console.WriteLine("list.Select(...) ending"); | |
// You will notice that you will see the "list.Select(...) starting" immediately followed by | |
// the "list.Select(...) ending" in your output. | |
// | |
// This is because the expressions in Select statements are lazy and only evaluated as they | |
// are enumerated. | |
// | |
// You should expect to see the logged lines when we enumerate them as per | |
// the below lines. | |
foreach (var item in projected) | |
{ | |
Console.WriteLine("{0}, {1} was enumerated", item.Original, item.Projection); | |
} | |
// When we call ToList() we are forcing the framework to enumerate our list and put the | |
// results in memory. | |
// We will see the logging from each select statement between the "projected.ToList() start" | |
// and "projected.ToList() end" log statements | |
Console.WriteLine("projected.ToList() start"); | |
var projectedList = projected.ToList(); | |
Console.WriteLine("projected.ToList() finished"); | |
// When we enumerate our in-memory list we will not see the intermediate logged statements in our | |
// select blocks. This is because we have forced evaluation with ToList(), in this example code | |
// we have simply moved the evaluation of our Select to before we enumerate but also forced that | |
// memory is allocated for us. This is unneccesary if we only wanted to enumerate the collection | |
// once | |
foreach (var item in projectedList) | |
{ | |
Console.WriteLine("{0}, {1} was enumerated", item.Original, item.Projection); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list.Select(...) starting | |
list.Select(...) ending | |
20 was selected | |
40 was selected | |
{ Original = 0, Projection = 20 }, 40 was enumerated | |
21 was selected | |
42 was selected | |
{ Original = 1, Projection = 21 }, 42 was enumerated | |
22 was selected | |
44 was selected | |
{ Original = 2, Projection = 22 }, 44 was enumerated | |
23 was selected | |
46 was selected | |
{ Original = 3, Projection = 23 }, 46 was enumerated | |
24 was selected | |
48 was selected | |
{ Original = 4, Projection = 24 }, 48 was enumerated | |
25 was selected | |
50 was selected | |
{ Original = 5, Projection = 25 }, 50 was enumerated | |
26 was selected | |
52 was selected | |
{ Original = 6, Projection = 26 }, 52 was enumerated | |
27 was selected | |
54 was selected | |
{ Original = 7, Projection = 27 }, 54 was enumerated | |
28 was selected | |
56 was selected | |
{ Original = 8, Projection = 28 }, 56 was enumerated | |
29 was selected | |
58 was selected | |
{ Original = 9, Projection = 29 }, 58 was enumerated | |
30 was selected | |
60 was selected | |
{ Original = 10, Projection = 30 }, 60 was enumerated | |
projected.ToList() start | |
20 was selected | |
40 was selected | |
21 was selected | |
42 was selected | |
22 was selected | |
44 was selected | |
23 was selected | |
46 was selected | |
24 was selected | |
48 was selected | |
25 was selected | |
50 was selected | |
26 was selected | |
52 was selected | |
27 was selected | |
54 was selected | |
28 was selected | |
56 was selected | |
29 was selected | |
58 was selected | |
30 was selected | |
60 was selected | |
projected.ToList() finished | |
{ Original = 0, Projection = 20 }, 40 was enumerated | |
{ Original = 1, Projection = 21 }, 42 was enumerated | |
{ Original = 2, Projection = 22 }, 44 was enumerated | |
{ Original = 3, Projection = 23 }, 46 was enumerated | |
{ Original = 4, Projection = 24 }, 48 was enumerated | |
{ Original = 5, Projection = 25 }, 50 was enumerated | |
{ Original = 6, Projection = 26 }, 52 was enumerated | |
{ Original = 7, Projection = 27 }, 54 was enumerated | |
{ Original = 8, Projection = 28 }, 56 was enumerated | |
{ Original = 9, Projection = 29 }, 58 was enumerated | |
{ Original = 10, Projection = 30 }, 60 was enumerated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment