Entity Framework (EF) is a popular Object-Relational Mapper (ORM) in C# that allows developers to interact with databases using LINQ (Language Integrated Query). LINQ enables querying the database in a clean, readable, and type-safe manner. However, writing LINQ queries efficiently is crucial to avoid performance bottlenecks. In this article, we’ll explore how to use LINQ … Continue reading How to Use LINQ with Entity Framework for Optimized Queries in C#
Category: LINQ
How to Use LINQ to Find Duplicates in a List in C#
Finding duplicates in a list is a common task when working with collections in C#. LINQ (Language Integrated Query) provides powerful methods to identify duplicate elements in a list efficiently. In this article, we’ll explore different LINQ techniques to detect duplicates in a list based on various conditions. Why Check for Duplicates? Example Data Set … Continue reading How to Use LINQ to Find Duplicates in a List in C#
LINQ Query to Filter Data with Null Values in C#
Filtering data with null values is a common requirement when working with collections in C#. Whether you’re processing data from databases, APIs, or files, handling null values effectively ensures reliable and error-free operations. LINQ (Language Integrated Query) provides an elegant way to filter data and manage null values in C#. Why Filter Null Values? Null … Continue reading LINQ Query to Filter Data with Null Values in C#
C# – LINQ Average Examples
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
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