LINQ Interview Questions with Answers

LINQ is a new way to exchange data from the database instead of general SQL queries. It is similar to SQL queries. This article describes some common important interview questions and answers in LINQ. Hope it will help you to build successful carrier.

What is LINQ?
LINQ stands for Language Integrated Query. It is a programming model. It is collection of standard query operators that provides the query facilities into .NET framework language (like C#, VB). It is the composition of general purpose standard query operators that allows working with data.

What are LINQ query expressions?
A LINQ query is called query expression. It consists of a combination of query clauses that identify the data sources for the query. It includes instructions for filtering, sorting, grouping, or joining to apply to the source data. It is similar to the SQL syntax. It indicates what information should be retrieved from the data source.

Write the basic syntax of a LINQ query in Visual Basic or C#.
The basic syntax of a LINQ query starts with the From clause and ends with the Select or Group By clause. We can also use the Where, Order By, and Order By Descending clauses.

What are the basic steps to execute a LINQ query?

  • Obtain the data source (SQL database or XML file)
  • Create a query
  • Execute the query

In which statement the LINQ query is executed?
A LINQ query is executed in the For Each statement in VB and in the foreach statement in C#.

What is DataContext?
To communicate with a database we must need to create a connection. In LINQ, this connection is created by a DataContext. It submits and retrieves object to database. It converts objects to SQL queries or vice versa.

What is a Lambda expression?
A Lambda expression is an Anonymous Function. It can contain expressions and statements. It mostly used to create delegates or expression tree types. It is helpful for writing LINQ query expression. It uses lambda operator => and read as ‘goes to’ operator. Left side of this operator specifies the input parameters and right sides contain the expression or statement.
Example: x => x * x
x is a parameter and returns the value of x squared

What is Object Relational Designer (0/R Designer)?
The 0/R Designer is one kind of designing interface that provides a visual design surface to create LINQ to SQL entity classes and associations (relationships) that are based on objects in a database.

What is PLINQ?
PLINQ stands for Parallel Language Integrated Query. It is a parallel implementation of LINQ. In PLINQ a query can be executed by using multiple processors. It is used where data grows in a hurry (telecom industry or where data is heterogeneous).

How LINQ is beneficial than Stored Procedures?
LINQ has good advantage over stored procedures:

  • Debugging
    Debuggingthe stored procedure is hard. With visual studio’s debugger we can easily debug LINQ queries.
  • Deployment
    LINQ deployment is easy. For SP we need to provide scripts but in LINQ everything’s gets compiled into a single DLL.
  • Type Safety
    LINQ is type safe. Errors are checked at compile time.

Give an example of LINQ select query
An example of LINQ query that returns CustomerID, CustomerName, Address from Customers table with gender male is given bellow:

MyLinqDataClassesDataContext db = new MyLinqDataClassesDataContext();
var result = from c in db.Customers
             where c.Gender == 'M'
             orderby c.CustomerName ascending
             select new { c.CustomerID, c.CustomerName, c.Address };

Give an example of LINQ join query
An example of LINQ query that returns CustomerID, CustomerName, OrderDate, Order Amount from Customer and Orders table is given bellow:

MyLinqDataClassesDataContext db = new MyLinqDataClassesDataContext();
var result = from c in db.Customers
             join o in db.Orders on c.CustomerID equals o.CustomerID
             where c.CustomerID == 1
             select new { c.CustomerID, c.CustomerName, o.OrderDate, o.Amount };

Write the query to return a generic list in LINQ
An example of LINQ query that creates a generic list of  Customer table is given bellow:

MyLinqDataClassesDataContext db = new MyLinqDataClassesDataContext();
var result = from c in db.Customers                      
             select new { c.CustomerID, c.CustomerName };

IList<object> items = new List<object>();
foreach (var r in result)
{
    items.Add(r);
}

Give an example of LINQ insert query
An example of LINQ query that insert data into Students table  is given bellow:

MyLinqDataClassesDataContext db = new MyLinqDataClassesDataContext();
Student oStudent = new Student();
oStudent.StudentName    = "Mr. KK Modi";
oStudent.Address        = "Mumbai";
oStudent.Phone          = "9912548";
db.Students.InsertOnSubmit(oStudent);
db.SubmitChanges();

Give an example of LINQ update query
A sample LINQ query to update Students tables is given bellow:

MyLinqDataClassesDataContext db = new MyLinqDataClassesDataContext();
var result = (from S in db.Students
              where S.StudentID == 2
              select S).Single();

result.StudentName  = "Mr. John Abrar";
result.Address      = "6/2 Park Road";
result.Phone        = "9658745";
db.SubmitChanges();

Give an example of LINQ delete query
A sample LINQ query to delete data from Students table is given bellow:

MyLinqDataClassesDataContext db = new MyLinqDataClassesDataContext();
var result = (from S in db.Students
              where S.StudentID == 3
              select S).Single();

db.Students.DeleteOnSubmit(result);
db.SubmitChanges();

How to call stored procedure in LINQ?
Consider we have two stored procedure TestProcedure1, and TestProcedure2. The procedure TestProcedure1 has no parameter, but TestProcedure2 has two integer type parameters.  Sample LINQ query that call a stored procedure (SP) is given bellow:

MyLinqDataClassesDataContext db = new MyLinqDataClassesDataContext();
GridView1.DataSource = db.TestProcedure1();
GridView1.DataBind();

GridView2.DataSource = db.TestProcedure2(1, 5);
GridView2.DataBind();

Comments

  1. By McCulloch

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *