Language intergraded query (LINQ) is one of the most popular data access mechanism used in middle tier; we also can use the query with html code in razor view, here are some very important LINQ interview questions and answers.
what is LINQ? Explain
LINQ is Language Integrated Query, a collection of standard query operators
which provides query facilities into.NET framework language like C#.
LINQ simplify to communicate between the world of data and world of objects.
What is the advantage of using LINQ over Stored Procedures
- Type Safety: LINQ is type safe, so queries errors are type checked at compile time
Debugging: It is easy to debug LINQ during development, when
debugging stored procedure with GUI data will be time consuming
There is no additional effort required to deploy LINQ, when store procedure takes another complete process in deployment.
How many types of LINQ?
- LINQ to Entities
- LINQ to SQL
- LINQ to Objects (System.Data.DLinq.dll)
- LINQ to Dataset
- LINQ to XML (System.XML.XLinq.dll)
How many types of join in linq?
Four types of Joins in LINQ
- INNER JOIN
- GROUP JOIN
- LEFT OUTER JOIN
- CROSS JOIN
What is the disadvantage of using LINQ over Stored Procedures
Stored Procedures is precompiled statement, that’s why Stored Procedures execute faster than LINQ
What is DataContext classes in LINQ?
DataContext class acts as a bridge between SQL Server database and the LINQ to SQL.
For accessing the database and also for updating data in the database,
it has property for connections string and many built-in functions.
what are the difference between Skip() and SkipWhile() extension method?
SkipWhile (): It will continue to skip the elements as far as the input condition is true.
and return all remaining elements if the condition is false
Skip() : It will take an integer argument and from the given IEnumerable it skips the top n numbers
Here are some more info about Skipwhile in LINQ query.
What is a Lambda expression?
A Lambda expression is nothing but an Anonymous Function, can contain expressions and statements
What is the extension of the file, when LINQ to SQL is used
extension of the file is .dbml
What is the benefit of using LINQ on Dataset?
We use LINQ to Dataset is to run strongly typed queries on Dataset.
If we want to combine the results from multiple Datasets,
or we want to take a distinct value from the Dataset, then it is good to use LINQ.
Normally we can use SQL queries to run on the database to populate the Dataset,
But we cannot use SQL query on a Dataset to retrieve a particular values, for this situation we need to use ADO.NET functionalities. But, in case of LINQ, it provides more dignified way of querying the Dataset and provides some new features as compared to ADO.NET.
Difference between XElement and XDocument?
Both are the classes defined by System.Xml.Linq namespace
XElement class represents an XML fragment
XDocument class represents an entire XML document with all associated meta-data.
why SELECT clause comes after FROM clause in LINQ?
LINQ is used with programming language like C#, in any programming language it requires any variable to be declared first.
"FROM" clause of LINQ query defines the range or conditions to select records.
So, FROM clause must appear before SELECT in LINQ.
variableName.Select(s => s.Name);
How standard query operators useful in LINQ?
- Get a total count of elements in the collection
- getting the results of a collection in Order
- Computing average
- Joining two collections based on matching keys
- Filter the results
How to change record order in LINQ ?
Here is simple example, we can fetch all clients in Descending Order
model.EmployerViews = dto.GetAllClientViews()
.OrderByDescending(c => c.CreateDate)
What is the difference between FirstOrDefault and First ?
FirstOrDefault will return null if no matching record found.
First will throw an error if no matching record found.
take a loot at FirstOrDefault in LINQ c# example.
Tell me the exact difference between IQueryable and IEnumerable interface ?
IEnumerable<T> is applicable for in-memory data querying,
and in contrast IQueryable<T> allows remote execution,
like web service or database querying.
You should read more about IEnumerable and IQueryable in LINQ example.