Average is a LINQ functionality which calculates average value of given collection. It works with both collections of primitive values and complex objects. Below article provides example of each one. List of integers var items = new List<int>() { 1, 2, 3, 4, 5 }; 1 var items = new List<int>() { 1, 2, 3, … Continue reading C# – LINQ Average Examples
Category: LINQ
C# – LINQ Sum Examples
Sum is a LINQ functionality which calculates total number of numeric items of a collection. In this article you can find examples of usage with list of primitive types (in our case integers) and also list of complex types. List of integers var items = new List<int>() { 1, 2, 3, 4, 5 }; 1 … Continue reading C# – LINQ Sum Examples
C# – LINQ GroupBy Examples
GroupBy is a LINQ functionality which allows to group items from a collection based on a given key. It’s an equivalent to SQL’s GROUP BY clause. In this article we’ll show various examples of usage, starting from the simplest grouping by one key, through grouping by multiple keys, custom result selector and all of that … Continue reading C# – LINQ GroupBy Examples
C# – LINQ Join Examples
Join is a LINQ functionality to combine two collections and produce a single result set. Connection happens by comparing items from both series. When there is a match then such pair is one of the output elements. Let’s consider following example. We’ve got two separate collections: countries and cities, which store objects of Country and … Continue reading C# – LINQ Join Examples
C# – How to get unique items from list
The easiest way of getting unique items from list is LINQ’s Distinct() method. In this article you’ll see how to use it with both built-in types (like collection of integers) and custom types (like collection of complex type objects). Get unique values from collection of integers Let’s start with creating example list of items where … Continue reading C# – How to get unique items from list
C# – LINQ Any Examples
Any is LINQ functionality to validate whether collection contains at least one element which meets given criteria. Let’s start with example collection of integers: var integers = new List<int>() { 36, 10, 4, 23, 1 }; 1 var integers = new List<int>() { 36, 10, 4, 23, 1 }; First call the Any function without any … Continue reading C# – LINQ Any Examples
C# – LINQ First Examples
First is LINQ functionality to return first item of the collection or throw exception if such item does not exist. First is overloaded method which can be used with either zero or one parameter. The first option just returns first element and the second one allows to define condition which needs to be met. 1. Collection … Continue reading C# – LINQ First Examples
C# – LINQ Take Example
Take is LINQ functionality to get given number of elements from the beginning of a collection. Below you can find out how to use it. We’ll start with creating example list of colors: var colors = new List<string>() { “red”, “green”, “blue”, “pink”, “grey” }; 1 var colors = new List<string>() { “red”, “green”, “blue”, … Continue reading C# – LINQ Take Example
C# – LINQ Skip Example
Skip is LINQ functionality to filter collection by omitting given number of first elements. Below is an example how to use it. Let’s create a simple collection of strings: var animals = new List<string>() { “dog”, “cat”, “lion”, “duck”, “fish” }; 1 var animals = new List<string>() { “dog”, “cat”, “lion”, “duck”, “fish” }; Now … Continue reading C# – LINQ Skip Example
C# – LINQ FirstOrDefault Examples
FirstOrDefault is a LINQ functionality to return first element of the collection or default value if requested item does not exist. In case of collection of reference type objects the default is null, whilst in case of value types the default depends on the particular type (e.g. for int it is 0). FirstOrDefault is overloaded … Continue reading C# – LINQ FirstOrDefault Examples