Getting Started with LINQ – Part 2

Hi Friends,

In this section, we’ll proceed further and look different projection strategies. So, the 1st case which i am using here is the Object Initializers. With C# Object Initializers, we can project the same into more complex types. For example, suppose, as a first step in a query, we want to strip vowels from a list of names while still keeping the original versions alongside, for the benefit of subsequent queries. Therefore, we can write as shown below

6th

However, Anonymous Types allow you to structure your intermediate results without writing special classes. So, here we can remove TempProjectionItem as shown below in the example.

7th8th

The let keyword introduces a new variable alongside the range variable. With let, we can write a query extracting strings whose length, excluding vowels, exceeds two characters, as follows:

8th

The compiler resolves a let clause by projecting into a temporary anonymous type that contains both the range variable and the new expression variable. Thanks for joining me. In the next section, we’ll delve further and check other pieces as well. Till then stay tuned and happy coding

Thanks,
Rahul Sahay
Happy Coding

1,754 total views, no views today