What is LINQ in C#?
LINQ stands for Language Integrated Query. It provides a consistent, strongly typed way to query collections, arrays, databases, XML, and other data sources directly from C#.
C# interview questions covering linq & querying.
Open a question for the full answer, code and interview guidance.
LINQ stands for Language Integrated Query. It provides a consistent, strongly typed way to query collections, arrays, databases, XML, and other data sources directly from C#.
LINQ supports query syntax and method syntax. Query syntax resembles SQL, while method syntax uses extension methods and lambda expressions.
Deferred execution means a LINQ query is defined first but is not necessarily executed immediately. Execution occurs when the result is enumerated, for example by foreach, ToList, ToArray, First, Count, or another terminal operation.
Where filters elements based on a condition. Select transforms or projects each element into another shape or value.
First returns the first matching element and throws an exception if no element exists. FirstOrDefault returns the first matching element or the default value when no element exists.
Any checks whether at least one element exists or satisfies a condition. Count calculates the number of matching elements.
These LINQ operators sort sequences. OrderBy sorts by the primary key in ascending order, while OrderByDescending sorts in descending order.
GroupBy organizes elements into groups based on a key. Each group has a Key and a sequence of matching elements.
Join combines elements from two sequences using matching keys, similar to an SQL INNER JOIN.
Select projects each input element into one output element or object. SelectMany projects each input element into a sequence and then flattens those nested sequences into one sequence.