When you merge two dictionaries, you can either merge them in-place, or create a new dictionary and copy the values over to it. The following extension method does an in-place merge of two dictionaries. It puts items from the right dictionary into the left dictionary. When duplicate keys exist, it’s keeping the value from the … Read more C# – Merge two dictionaries in-place
To sort a list, except for the first element, you need to: Separate the first element from the rest of the elements. Sort the rest of the elements. Merge the first element with the sorted list. This can be accomplished in a single line using Linq methods: Example – sorting a list of people Let’s … Read more C# – Sort all but first element
Use OrderBy() and ThenBy() when you want to sort by multiple fields, like this: Note: These are Linq extension methods, so make sure to add a reference to System.Linq. Ascending vs Descending order By default, OrderBy() and ThenBy() sort in ascending order. If you want to sort by descending order, use the Descending version of … Read more C# – How to sort by multiple fields
Problem When you call .First() on an empty IEnumerable, you get the following exception: System.InvalidOperationException: Sequence contains no elements Solution Option 1 – Use .FirstOrDefault() instead of .First() When the IEnumerable is empty, .FirstOrDefault() returns the default value for the type. For reference types this returns null. For value types this returns 0 or that … Read more System.InvalidOperationException: Sequence contains no elements
Problem You’re getting the following error when compiling: Could not find an implementation of the query pattern for source type ‘Your Type’. ‘Select’ not found. Solution The error message is odd, but the solution is simple. Add this: I’ve only ran into this in older versions of VS.