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# – How to generate Guid
.NET Framework provides built-in Guid structure which allows to generate unique identifier. The usage is very simple and requires to call NewGuid function on Guid structure. Guid guid = Guid.NewGuid(); 1 Guid guid = Guid.NewGuid(); Returned object type is Guid however we can easily convert it to string. var guidString = guid.ToString(); 1 var guidString = … Continue reading C# – How to generate Guid
C# – Object to JSON Examples
In this article you can find how to convert C# object into JSON using Json.NET (Newtonsoft.Json) library. It is available as a NuGet package for free, so you can easily install it from nuget.org repository. Let’s create an example class which object will be converted into JSON. public class Place { public string Name { get; … Continue reading C# – Object to JSON Examples
C# – JSON to Object Examples
In this article you can find examples how to convert JSON into C# object using Json.NET (Newtonsoft.Json) library. It is available as a NuGet package for free, so you can easily install it from nuget.org repository. Let’s create a JSON which will be used in below examples. { Forename: “John”, Surname: “Smith”, Age: 40, IsMarried: … Continue reading C# – JSON to Object 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
C# – LINQ Select Examples
Select is a LINQ functionality to define a data format of the query results. Each of below examples is presented in C# with both Lambda and Query expression. Let’s create a Person class. public class Person { public string Forename { get; set; } public string Surname { get; set; } public DateTime DateOfBirth { … Continue reading C# – LINQ Select Examples
C# – LINQ Where Examples
Where is a LINQ functionality to filter data in a query with given criteria. Each of below examples is presented in C# with both Lambda and Query expression. 1. Collection of strings – single condition Query collection to get items which start with “b”. var colors = new List<string>() { “red”, “green”, “blue”, “black”, “white” … Continue reading C# – LINQ Where Examples