Friday, 13 December 2013

LINQ Questions

1. 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.

2. How LINQ is beneficial than Stored Procedures?

  • 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.
  • 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.
  • 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

3. 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.

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

The extension of the file is .dbml

5. 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.

6. What is the disadvantage of LINQ over stored procedures?

The disadvantage with LINQ is, it is not a precompiled statement where as stored procedures are precompiled.
In case of LINQ the queries need to be compile before the execution. So according to this,
I can say stored procedures are faster in performance as compared to LINQ.

7. What are Quantifiers?

They are LINQ Extension methods which return a Boolean value 
1)All
2)Any
3)Contains
4)SequenceEqual 

8. Difference between IQueryable and IEnumerable interface ?

IEnumerable

  1. IEnumerable exists in System.Collections Namespace.
  2. IEnumerable can move forward only over a collection, it can’t move backward and between the items.
  3. IEnumerable is best to query data from in-memory collections like List, Array etc.
  4. While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
  5. IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
  6. IEnumerable supports deferred execution.
  7. IEnumerable doesn’t supports custom query. 
  8. IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
  9. Extension methods supports by IEnumerable takes functional objects.

MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);
 
*Notice that in this query "top 10" is missing since IEnumerable filters records on client side.

IQueryable 

  1. IQueryable exists in System.Linq Namespace.
  2. IQueryable can move forward only over a collection, it can’t move backward and between the items.
  3. IQueryable is best to query data from out-memory (like remote database, service) collections.
  4. While query data from database, IQueryable execute select query on server side with all filters.
  5. IQueryable is suitable for LINQ to SQL queries.
  6. IQueryable supports deferred execution.
  7. IQueryable supports custom query using CreateQuery and Execute methods.
  8. IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
  9. Extension methods supports by IEnumerable takes expression objects means expression tree.
MyDataContext dc = new MyDataContext ();
IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);
 
*Notice that in this query "top 10" is existing since IQueryable executes query in SQL server with all filters.

Points of Interest  

  1. IEnumerable<T> represents a forward-only cursor of T, .NET 3.5 added extension methods that included the LINQ standard query operators like Where and First, with any operators that require predicates or anonymous functions taking Func<T>.
  2. IQueryable<T> implements the same LINQ standard query operators, but accepts Expression<Func<T>> for predicates and anonymous functions. Expression<T> is a compiled expression tree, a broken-up version of the method ("half-compiled" if you will) that can be parsed by the queryable's provider and used accordingly.

9. What is the difference between FirstOrDefault() and SingleOrDefault() extension method in LINQ ?

FirstOrDefault() = gets the first item that matches a given criteria. 
SingleOrDefault() = if you specify this extension method that means you are specifically saying that there can be only one value that matches the criteria. If there are more then 1 value that matches the criteria, throw an exception.

10. What is the difference between First() and Single() extension methods in LINQ ?

• First() - There is at least one result, an exception is thrown if no result is returned. 
• Single() - There is exactly 1 result, no more, no less, an exception is thrown if no result is returned.

11. Write a query to get the single employee name when there are many employees whose name is "test" in the database ?

var employee = (from h in contextobject.Employee
                where h.EmployeeName == "test"
                select h).FirstOrDefault<Employee>();

12. Write a query to get the list of all employees whose name is "test" ?

var employeeList = (from h in context.Employee
                    where h.EmployeeName == "test"
                    select h).ToList<Employee>();

13. What are the three main components of LINQ or Language Integrated Query?

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

14. 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

15. 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

16. What are the four LINQ Providers that .NET Framework ships?

1. LINQ to Objects - Executes a LINQ query against a collection of objects
2. LINQ to XML - Executes an XPATH query against XML documents
3. LINQ to SQL - Executes LINQ queries against Microsoft SQL Server.
4. LINQ to DataSets - Executes LINQ queries against ADO.NET DataSets.




No comments:

Post a Comment