LINQ Exclusive Interview Questions and Answers (27) - Page 2

  • A joint initiative from DotNetFunda.Com and Questpond.
  • A one stop place on the internet to get trusted and proven Interview Questions (based on more than a decade of experience in this field) to ensure the success of the candidates.
  • You will find almost all Interview questions from Questpond here and this list is growing day by day with all latest and updated interview questions.

27 records found.

Get 650+ Questpond's Interview videos on discount

How can we use XML files to map stored procedures with .NET classes?

In case you have stored procedures in your project you can use ‘Function’ XML element to define your stored procedure name in the XML file. The client code does not change for binding the datacontext and XMLMappingsource object.

<?xml version="1.0" encoding="utf-8"?>

<Database Name="TstServer" xmlns="">
<Table Name="dbo.Customer" Member="WebAppMappingXML.clsCustomer">
<Type Name="WebAppMappingXML.clsCustomer">
<Column Name="CustomerId" Member="CustomerId" />
<Column Name="CustomerName" Member="CustomerName" />
<Column Name="CustomerCode" Member="CustomerCode" />
<Function Name="dbo.sp_getCustomerCode" Method="getCustomerByCode">
<Parameter Name="CustomerCode" Parameter="" />
<ElementType Name="clsCustomer" />

List 2 important features that a good concurrency system should support?

A good concurrency system should at least provide two important features: -

• It should be able to detect if any concurrency violation has taken place.

• Once concurrency violation has been detected it should provide various ways of how to handle the concurrency violation.

Does LINQ supports optimistic concurrency?

LINQ entity forms the main heart of LINQ engine. All the data from database is fetched using ‘datacontext’ and given to LINQ objects. Once the data is fetched in the LINQ objects the LINQ objects gets disconnected from the database, in other words LINQ is a disconnected architecture. Due to disconnected architecture the only type of locking we can support is optimistic locking.

Good news !!! LINQ supports optimistic concurrency by default.

Do optimistic concurrency works?

For that let us do a small sample test to show LINQ really support optimistic concurrency default.

So we will do the following: -

• We will open a data context, fetch a record and modify the record. We will not be calling ‘SubmitChanges’ , in other words we will not be making final update to physical database.

• We will then open a new ‘DataContext’ connection, fetch the same record and update the record physically in the database.

• Then we will come back to the same old record to call the physical update using ‘SubmitChanges’. In this way we will be able to simulate concurrency violation.

So below goes the code.

We first fetch the record and change the customer name but we have not called the physical database commit yet.

DataContext objContext = new DataContext(strConnectionString);

clsCustomer objCustomer = objContext.GetTable<clsCustomer>().First<clsCustomer>();
objCustomer.CustomerName = "Iwanttochange";


We then call a different method which will change this record by which we can simulate concurrency simulation.



The ‘SomeOneChangedData’ method is a simple code which fetches the same record and changes it.

private void SomeOneChangedData()

DataContext objContext = new DataContext(strConnectionString);
clsCustomer objCustomer = objContext.GetTable<clsCustomer>().First<clsCustomer>();
// changing to some random customer name
objCustomer.CustomerName = new Random().NextDouble().ToString();


We then come back to the old ‘DataContext’ object and try to call the ‘SubmitChanges’ method. This call will create concurrency violation as ‘SomeOneChangedData’ method has already changed data.



If you are in debug mode you should see ‘ChangeConflictException’ exception thrown as shown in the below figure.

If you want to really see the exception you can run the code without try and catch exception block. You should see something as shown in the below figure.

Various ways of handling concurrency violation in LINQ?

LINQ gives three ways by which we can handle concurrency conflicts. To handle concurrency conflicts we need to wrap the LINQ to SQL code in a ‘TRY’ block and catch the ‘ChangeConflictException’. We can then loop through the ‘ChangeConflicts’ collection to specify how we want the conflict to be resolved.

catch (ChangeConflictException ex)

foreach (ObjectChangeConflict objchangeconf in objContext.ChangeConflicts)

There are 3 ways provided by LINQ system to handle concurrency conflicts:-
• KeepCurrentValues :- When this option is specified and concurrency conflicts happen LINQ keeps call the LINQ entity object values as it is and does not push the new values from the database in to the LINQ object.
• OverwriteCurrentValues :- When this option is specified the current LINQ object data is replaced with the database values.
• KeepChanges :- This is the most weird option but can be helpful in some cases. When we talk about classes it can have many properties. So properties which are changed are kept as it is but the properties which are not changed are fetched from the database and replaced.

We need to use the ‘RefereshMode’ to specify which options we need as shown in the below code snippet.

What are the error reporting options during concurrency conflicts?

LINQ concurrency system lets you specify how you want the conflicts to be reported. LINQ system has given 2 ways to report conflicts:-

ContinueOnConflict :- This option says to the LINQ engine that continue even if there are conflicts and finally return all conflicts at the end of the process.

FailOnFirstConflict :- This option says stop as soon as the first conflict occurs and return all the conflicts at that moment. In other words LINQ engine does not continue ahead executing the code.

Both these options can be provided as a input in ‘SubmitChanges’ method using the ‘ConflictMode’ enum. Below is the code snippet of how to specify conflict modes.


.NET/ASP.NET interview Questions - How to do simple insert, update and delete operation using LINQ?

Let us assume that we have the following table of Student with their respective fields.


First open visual studio > New > project > select ASP.NET empty web application.

Add > New item > select Web Form and create a simple form like below.


Once you have done creating the form, now you have to add LINQ to SQL Classes in your project.

Add > New item > select LINQ to SQL Classes.

As soon as you click on add a new window will appear from that just click on server explorer expand the data connection and select your database then drag and drop the table on the .dbml file like below diagram.


Now you have to add a class library file in your project.

Add > New item > select Class.



privatestring _Name;
privatestring _Add;

publicstring Name
get{ _Name = value;}                                                  
get{return _Name;}
publicstring Add
set{ _Add = value; }
get{return _Add;}

Write code for insert update and delete with their respective buttons.

For adding record.

protectedvoidbtnAdd_Click(object sender, EventArgs e)

DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
StudentObjStud = newStudent();
ObjStud.StudentAdd = TextBox2.Text;
ObjStud.StudentName = TextBox1.Text.ToString();
Response.Write("Record Successfully Inserted");


For updating record.
protectedvoidbtnUpdate_Click(object sender, EventArgs e)

DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
StudentObjStud = newStudent();
string Name1 = TextBox1.Text;
string Address = TextBox2.Text;
var Update = ObjDataContext.Students.Single(p =>p.StudentName == Name1);
Update.StudentAdd = Address;
Response.Write("Record Successfully Updated");


For deleting record.
protectedvoidbtnDelete_Click(object sender, EventArgs e)

DataClasses1DataContextObjDataContext = newDataClasses1DataContext();
StudentObjStud = newStudent();
string Name1 = TextBox1.Text.ToString();
var delete = ObjDataContext.Students.Single(s =>s.StudentName == Name1);
Response.Write("Record Successfully deleted");


You will be also interested in watching the below video, which are also asked in most of the interviews and favourable question of interviewers.

More LINQ Interview Questions & Answers here

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Exclusive Interview Questions and Answers Categories