Click here to Skip to main content
15,887,328 members
Articles / LINQ

Getting Started with LINQ – Part 2

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Jun 2015CPOL 5.5K   1  
Different projection strategies in LINQ

In this section, we’ll proceed further and look at different projection strategies. So, the first 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:

JavaScript
void Main()
{
	var names = new[] { "Rahul", "Sahay", "Tom", "Dave", "Hari" }.AsQueryable();
	
	IEnumerable<TempProjectionItem> temp =
		from n in names
		select new TempProjectionItem
		{
			Original  = n,
			Vowelless = n.Replace ("a", "").Replace ("e", "").Replace ("i", "").Replace 
                         ("o", "").Replace ("u", "")
		};
		
	temp.Dump();
	  
	}
	class TempProjectionItem
	{
		public string Original;      // Original name
		public string Vowelless;   // Vowel-stripped name
		
}

// Define other methods and classes here

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.

JavaScript
void Main()
{
	var names = new[] { "Rahul", "Sahay", "Tom", "Dave", "Hari" }.AsQueryable();
	
	var intermediate = from n in names
	select new
	{
		Original = n,
		Vowelless = n.Replace ("a", "").Replace ("e", "").Replace ("i", "").Replace 
                    ("o", "").Replace ("u", "")
	};
	
	(
		from    item in intermediate
		where   item.Vowelless.Length > 2
		select  item.Original
	)
	.Dump();
	
	// With the into keyword we can do this in one step:
	
	(
		from n in names
		select new
		{
			Original = n,
			Vowelless = n.Replace ("a", "").Replace ("e", "").Replace ("i", "").Replace 
                        ("o", "").Replace ("u", "")
		}
		into   temp
		where  temp.Vowelless.Length > 2
		select temp.Original
	)
	.Dump ("With the 'into' keyword");
}

// Define other methods and classes here

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:

JavaScript
void Main()
{
	var names = new[] { "Rahul", "Sahay", "Tom", "Dave", "Hari" }.AsQueryable();
	(
		from n in names
		let vowelless = n.Replace ("a", "").Replace ("e", "").Replace ("i", "").Replace 
                         ("o", "").Replace ("u", "")
		where vowelless.Length > 2
		orderby vowelless
		select n		       // Thanks to let, n is still in scope.
	)
	.Dump();
}

// Define other methods and classes here

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!

This article was originally posted at http://myview.rahulnivi.net?p=2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Publicis Sapient
India India
Hey there, it's Rahul Sahay! I'm thrilled to be a platform specialist at Publicis Sapient, where I get to work on some exciting projects. I've been honing my skills in various aspects of the software development life cycle for more than 15 years, with a primary focus on web stack development. I've been fortunate to have contributed to numerous software development initiatives, ranging from client applications to web services and websites. Additionally, I enjoy crafting application architecture from scratch, and I've spent most of my time writing platform agnostic and cloud agnostic code. As a self-proclaimed code junkie, software development is more than just a job to me; it's a passion! And I consider myself lucky to have worked with an array of cutting-edge technologies, from .NetCore to SpringBoot 3, from Angular to React, and from Azure to AWS and many more cousin technologies...

- 🔭 I’m currently working @ below tech stacks
- Microservices,
- Distributed Systems,
- Spring Boot
- Spring Cloud
- System Design,
- Docker,
- Kubernetes,
- Message Queues,
- ELK Stack
- DotNetCore,
- Angular,
- Azure

- 💬 Ask me anything about my articles [My View](https://myview.rahulnivi.net/)
- 📫 How to reach me: [@rahulsahay19](https://twitter.com/rahulsahay19)
- 📫 Github: [@rahulsahay19](https://github.com/rahulsahay19)

Comments and Discussions

 
-- There are no messages in this forum --