Wednesday, May 19, 2010

LINQ Interview Questions

Which assembly represents the core LINQ API?
System.Query.dll assembly represents the core LINQ API.

What is the use of System.Data.DLinq.dll?

System.Data.DLinq.dll provides functionality to work with LINQ to SQL.

What is the use of System.XML.XLinq.dll?
System.XML.XLinq.dll contains classes to provide functionality to use LINQ with XML.

Why can't datareader by returned from a Web Service's Method

Cos, it's not serializable

What is the LINQ file extension that interacts with Code Behind's objects.
its .dbml

What is the extension of the file, when LINQ to SQL is used?
The extension of the file is .dbml

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.

How LINQ is beneficial than Stored Procedures?
There are couple of advantage of LINQ over stored procedures.

1. Debugging - It is really very hard to debug the Stored procedure but as LINQ is part of .NET, you can use visual studio's debugger to debug the queries.

2. Deployment - With stored procedures, we need to provide an additional script for stored procedures but with LINQ everything gets complied into single DLL hence deployment becomes easy.

3. Type Safety - LINQ is type safe, so queries errors are type checked at compile time. It is really good to encounter an error when compiling rather than runtime exception!

What is LINQ?
It stands for Language Integrated Query. LINQ is collection of standard query operators that provides the query facilities into .NET framework language like C# , VB.NET.

What is a Lambda expression?
A Lambda expression is nothing but an Anonymous Function, can contain expressions and statements. Lambda expressions can be used mostly to create delegates or expression tree types. Lambda expression uses lambda operator => and read as 'goes to' operator.

Left side of this operator specifies the input parameters and contains the expression or statement block at the right side.

Example: myExp = myExp/10;

Now, let see how we can assign the above to a delegate and create an expression tree:

delegate int myDel(int intMyNum);
static void Main(string[] args)
{
//assign lambda expression to a delegate:
myDel myDelegate = myExp => myExp / 10;
int intRes = myDelegate(110);
Console.WriteLine("Output {0}", intRes);
Console.ReadLine();

//Create an expression tree type
//This needs System.Linq.Expressions
Expression myExpDel = myExp => myExp /10;
}
How do we write a LINQ query to search with criteria?
We need to put the where clause before the 'select' keyword.
var q1 = from clsCustomer Obj in objCustomer where Obj.customerCode == "001" select Obj;

How can do a join using LINQ query?

Below is the LINQ code snippet for creating joins between object collections. In this case we are creating a join on customer and orders. If you remember the order collection was contained in the customer class.

var q1 = from clsCustomer ObjCust in objCustomer
from clsOrder ObjOrder in ObjCust.Orders
select ObjCust;

How can we do a group by using LINQ query?

Below is the code snippet which shows how group by query is written using LINQ. You can see we have created first a temp variable i.e. 'GroupTemp' and then we have used the 'Select' clause to return the same.

var GroupCustomers = from ObjCust in objCustomer
group ObjCust by ObjCust.City into GroupTemp
select new {GroupTemp.Key,GroupTemp};


How can we do an order by using LINQ query?

Order by in LINQ is pretty simple. We just need to insert order by before the 'Select' query.
return from clsCustomer ObjCust in objCustomer
orderby ObjCust.City
select ObjCust;

Note:
The => operator has the same precedence as assignment (=) and is right-associative.

Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as Where.

No comments:

Post a Comment