Converting a sequence to a dictionary using the ToDictionary LINQ operator

Say you have a sequence of objects that you’d like to convert into a Dictionary for efficient access by key. Ideally the objects have some kind of “natural” key for the dictionary such as an ID:

public class Singer
	public int Id { get; set; }
	public string FirstName { get; set; }
	public string LastName { get; set; }

IEnumerable<Singer> singers = new List<Singer>() 
			new Singer(){Id = 1, FirstName = "Freddie", LastName = "Mercury"} 
			, new Singer(){Id = 2, FirstName = "Elvis", LastName = "Presley"}
			, new Singer(){Id = 3, FirstName = "Chuck", LastName = "Berry"}
			, new Singer(){Id = 4, FirstName = "Ray", LastName = "Charles"}
			, new Singer(){Id = 5, FirstName = "David", LastName = "Bowie"}

Read more of this post

Convert a sequence of objects to a sequence of specific type in LINQ .NET

Say that you have some old style collection such as this:

ArrayList stringList = new ArrayList() { "this", "is", "a", "string", "list" };

You can easily turn this into a proper string list using the Cast operator:

Read more of this post

Ordering a .NET data sequence with various LINQ operators

A sequence of any object can be easily ordered with the following LINQ operators in .NET:

  • OrderBy
  • ThenBy
  • OrderByDescending
  • ThenByDescending

Consider the following sequence:

string[] bands = { "ACDC", "Queen", "Aerosmith", "Iron Maiden", "Megadeth", "Metallica", "Cream", "Oasis", "Abba", "Blur", "Chic", "Eurythmics", "Genesis", "INXS", "Midnight Oil", "Kent", "Madness", "Manic Street Preachers", "Noir Desir", "The Offspring", "Pink Floyd", "Rammstein", "Red Hot Chili Peppers", "Tears for Fears"					 , "Deep Purple", "KISS"};

Read more of this post

Selecting a subset of elements in LINQ C# with the TakeWhile operator

The TakeWhile extension method in LINQ is similar to Take. With Take you can specify the number of elements to select from a sequence. In the case of TakeWhile we can specify a condition – a boolean function – instead. The operator will take elements from the sequence while the condition is true and then stop.

Data collection:

Read more of this post

Concatenate two IEnumerable sequences in C# .NET

We can use the Range method to build two integer sequences as follows:

IEnumerable<int> firstRange = Enumerable.Range(10, 10);
IEnumerable<int> secondRange = Enumerable.Range(5, 20);

You can join the two sequences using the Concat method:

List<int> concatenated = firstRange.Concat(secondRange).ToList();
concatenated.ForEach(i => Debug.WriteLine(i));

You’ll see that the concatenated sequence holds all numbers from the first and the second sequence:

Read more of this post

Using the let keyword in .NET LINQ to store variables within a statement

It happens that we have a LINQ statement where we want to refer to partial results by variable names while expressing some computation. The “let” keyword lets us do that. Those who are familiar for the F# language already know that “let” is an important keyword to bind some value to a variable.

Suppose we have the following list of integers:

List<int> integers = new List<int>()
	5, 7, 4, 6, 10, 4, 6, 4, 5, 12

Read more of this post

Breaking up a collection into smaller fixed size collections with C# .NET

Occasionally you may need to break up a large collection into smaller size collections or groups. Consider the following integer list:

List<int> ints = new List<int>() { 1, 4, 2, 5, 2, 6, 5, 43, 6, 234, 645, 2, 12, 45, 13, 5, 3 };

The goal is to break up this list into groups of size 5 and if necessary an additional list for the remaining elements. The following generic method will do the trick:

Read more of this post


A great site

Elliot Balynn's Blog

A directory of wonderful thoughts

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: