Index

LINQ Interview Questions and Answers - Page 1

What is use of Distinct in a LINQ ?


It removes duplicate values from result set.

What is different between LINQ and Stored Procedure?


- We can debug LINQ as it is part of .Net, whereas Stored procedure can’t be.
- Deployment is easy with LINQ as everything compiled into DLL whereas with Stored procedure script is required for deployment.
- At compile time we can find error, if any in LINQ but it not possible with Stored procedure.

What is role of DataContext classes in LINQ?


DataContext acts as a bridge between a SQL Server database and the LINQ to SQL. It contains the connection string and functions for accessing the database and for also changing the data in the database.

In LINQ, ‘Select’ comes after ‘ From’, why?


In any programming language, variable defines first then conditions specifies. In LINQ also, all variables declared first by using ‘Select’ clause then ‘From’ clause is used for define conditions.

What is the extension of LINQ to SQL file ?


It is .dbml.

Disadvantage of LINQ over Stored procedure?


Stored procedure compiles one time and executed every time whereas LINQ compiled everytime , so Stored Procedure is faster as compare to LINQ.

How to retrieve first n rows from table using LINQ?


<code>var data = (from std in students
select std).Take(100);</code>
It fetches the first 100 rows and display it.

How to skip n rows from table using LINQ?


<code>var data = (from std in students
select std).Skip(100);</code>
It skips the first n rows from the table.

How to retrieve data from multiple tables?


If you want to retrieve data from multiple tables then use multiple from clauses instead of a join clause to access inner collections.

How to sort results using orderby clause?


When you want to sort results using orderby clause then always put where clause first so that data will be filtered as per condition there itself and then use orderby clause for sorting.
<code>var firstclassStudentsData = from std in students
where std.Class == "First"
orderby std.Name
select std;</code>
[note]In orderby you can mention ascending or descending or default.[/note]

How to enable/disable lazy loading?


Deferred loading or lazy-loading can be disabled or enabled on the data context by setting DeferredLoadingEnabled property.

If it is not required to load child data along with parent data then lazy loading should be false.

What is the Use of FirstOrDefault() in LINQ


when you are sure that data will contain single row or topmost row or default data then always better to use FirstOrDefault().

How to make LINQ class serializable?


If you want to make the LINQ class serializable, you'll need to mark all System.Data.Linq.EntitySet and System.Data.Linq.EntityRef fields with NonSerialized attribute and to mark with Serializable attribute the LINQ class.

What is Attach method in LINQ?


Attach method allows you to update entities that have changed outside the context.

Why Select clause comes after from clause in LINQ?


The reason is, LINQ is used with C# or other programming languages, which requires all the variables to be declared first.

From clause of LINQ query just defines the range or conditions to select records. So that’s why from clause must appear before Select in LINQ.

What is the benefit of using LINQ on Dataset?


The main objective of using LINQ to Dataset is to run strongly typed queries on Dataset.
Suppose we want to combine the results from two Datasets, or we want to take a distinct value from the Dataset, then it is advisable to use LINQ.

Normally you can use the SQL queries to run on the database to populate the Dataset, but you are not able to use SQL query on a Dataset to retrieve a particular values. To get this you 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.

What are the three main components of LINQ or Language INtegrated Query?


1. Standard Query Operators
2. Language Extensions
3. LINQ Providers

How are Standard Query Operators implemented in LINQ?


Standard Query Operators are implemented as extension methods in .NET Framework. These Standard Query Operators can be used to work with any collection of objects that implements the IEnumerable interface. A class that inherits from the IEnumerable interface must provide an enumerator for iterating over a collection of a specific type. All arrays implement IEnumerable. Also, most of the generic collection classes implement IEnumerable interface.

How are Standard Query Operators useful in LINQ?


Standard Query Operators in LINQ can be used for working with collections for any of the following and more.
1. Get total count of elements in a collection.
2. Order the results of a collection.
3. Grouping.
4. Computing average.
5. Joining two collections based on matching keys.
6. Filter the results

List the important language extensions made in C# to make LINQ a reality?


1. Implicitly Typed Variables
2. Anonymous Types
3. Object Initializers
4. Lambda Expressions

Similar Tags