DEV Community

Deependra kushwah
Deependra kushwah

Posted on

Few tricky Linq Interview Questions & Answers

What is deferred execution in LINQ?

Deferred execution is a key feature of LINQthat allows queries to be evaluated lazily, or only when their results are actually needed. Instead of executing the query immediately when it is defined, a LINQ query is only executed when it is enumerated, usually by calling a method such as ToList(), ToArray(), or FirstOrDefault().

Example of deferred execution

What is the difference between IQueryable and IEnumerable?

IQueryable and IEnumerable are two interfaces in LINQ that represent collections of data. The main difference between them is that IQueryable is designed for querying remote data sources (such as databases), while IEnumerable is designed for querying local in-memory data (such as collections).
*IEnumerable *
IEnumerable is the simpler of the two interfaces, and provides a basic set of methods for iterating over a collection of objects. It is used for in-memory collections such as arrays, lists, and dictionaries.

Example of IEnumerable
IQueryable
IQueryable, on the other hand, is used for querying remote data sources such as databases or web services. It extends IEnumerable and adds additional functionality for querying data in a way that is optimized for the specific data source.

Example Of IQueryable

What is the difference between Single() and First() in LINQ?

Both Single() and First() are LINQ extension methods used to retrieve a single element from a sequence. However, there are some key differences between them.

First() returns the first element in a sequence that satisfies a specified condition, or the first element in the sequence if no condition is specified. If the sequence is empty, First() throws an exception.
Single(), on the other hand, returns the only element in a sequence that satisfies a specified condition, or throws an exception if there is more than one element in the sequence that satisfies the condition. If no element satisfies the condition or the sequence is empty, Single() also throws an exception.

Read Complete Article
Crack Interview With 30 C# Linq Interview Questions & Answers

Top comments (0)