Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to…

Follow publication

Member-only story

C#: LINQ Expressions with Enumerable Class (Part 6)

--

These two methods perform similar tasks. Let’s take a closer look at what they do.

Concat

It appends the second sequence to the first sequence, resulting in a new sequence that contains all elements from both sequences in the order they are provided.

var numbers1 = new[] { 70, 30, 100 };
var numbers2 = new[] { 50, 20, 30 };

Console.WriteLine(string.Join(", ", numbers1.Concat(numbers2))); // 70, 30, 100, 50, 20, 30

Union

It combines two sequences into one, ensuring that the resulting sequence contains only distinct elements.

var numbers1 = new[] { 70, 30, 100 };
var numbers2 = new[] { 50, 20, 30 };

Console.WriteLine(string.Join(", ", numbers1.Union(numbers2))); // 70, 30, 100, 50, 20

Select

Select is one of the most important LINQ methods.

It projects each element of a collection into a new form. It applies a transformation function to each element of the source collection and returns a collection of the results.

This is cool because, for example, we can take a collection of strings and return a collection of instances of some class.

var names = new List<string>
{
"Whiskers"

--

--

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Written by Tomas Svojanovsky

I'm a full-stack developer. Programming isn't just my job but also my hobby. I like developing seamless user experiences and working on server-side complexities

No responses yet

Write a response