Posts

Showing posts with the label LINQ

LINQ Methods or concepts

 LINQ: In LINQ, several methods are used to retrieve single or multiple elements from a collection.  Key LINQ Methods for Element Retrieval 1. Single: - Returns the only element of a sequence, and throws an exception if there is not exactly one element. - Usage: Single() Example: var employee = employees.Single(e => e.Id == 1); 2. SingleOrDefault: - Returns the only element of a sequence, or a default value if the sequence is empty. Throws an exception if there is more than one element. - Usage: SingleOrDefault() Example: var employee = employees.SingleOrDefault(e => e.Id == 1); 3. First: - Returns the first element of a sequence, and throws an exception if the sequence is empty. - Usage: First() Example: var firstEmployee = employees.First(); 4. FirstOrDefault: - Returns the first element of a sequence, or a default value if the sequence is empty. - Usage: FirstOrDefault() Example: var firstEmployee = employees.FirstOrDefault(e => e.Id == 1); 5. Last: - Returns the l...

LINQ

LINQ: LINQ (Language Integrated Query) is a set of methods in .NET that allows you to query and manipulate data in a more declarative way using C# or other .NET languages. LINQ can be used to query various data sources, including collections in memory, databases, XML, and even remote data sources. Example:      You can use LINQ to query an array, list, or a database with SQL-like syntax but directly in C#. Types of LINQ:   1. LINQ to Objects: Queries collections such as arrays, lists, or other in-memory objects.   2. LINQ to SQL: Queries data in a SQL database via an ORM (Object-Relational Mapping), such as Entity Framework.   3. LINQ to XML: Used to query and manipulate XML data.   4. LINQ to Entities: Queries data in a database using Entity Framework. IEnumerable and IQueryable: IEnumerable:      Used for in-memory collections, supports deferred execution, and performs operations in memory without translating to an external query langu...

LINQ - Differences

 1. Queryable vs Enumerable in LINQ Queryable (found in System.Linq.Queryable) is used for LINQ queries that work with IQueryable collections. It is often used with databases or any collection that supports deferred execution (e.g., Entity Framework queries). The queries are translated into the provider's query language (like SQL). Enumerable (found in System.Linq.Enumerable) is used for LINQ queries on IEnumerable collections (like arrays, lists, or other in-memory collections). It is intended for querying collections that reside in memory and supports immediate execution. 2. Distinct vs GroupBy in LINQ Distinct: This method is used to return distinct (unique) elements from a collection, removing duplicates based on the equality of the elements. Example: var distinctNumbers = numbers.Distinct(); GroupBy: This method is used to group elements in a collection based on a key. It does not eliminate duplicates but groups them together. Example: var groupedNumbers = numbers.GroupBy(x =...