You are on page 1of 11

LINQ Difference FAQs-1

1.Difference between LINQ and SQL S.No 1 2 LINQ LINQ Stands for language integrated query. LINQ Statements are verified during compile time. To use LINQ we can depend upon our .Net Language syntaxes and also we can consume base class library functionalities. LINQ Statements can be debugged as they execute under framework environment. SQL . SQL SQL stands for Structured Query Language. SQL statements can be used in a application gets verified for their syntaxes only in the run time. To use SQL we need to be familiar with SQL syntaxes and also the pre-defined functions of SQL like MAX,MIN,LEN and SUBSTRING etc... As SQL statements execute on Database server debugging of the code is not possible.

2.Difference between LINQ to SQL and Entity Framework S.No 1 2 3 4 5 6 LINQ to SQL It only works with Sql server. Used for rapid application development. It does not support for complex type. It cannot generate db from model. Mapping type ( class to single table) We can query data using DataContext. Entity Framework It works with variety of db products. Cannot used for rapid application development. It provides support for complex type. It can generate db from model. Mapping type ( class to multiple tables) We can query data using esql,object services,entity client and linq to entities.

Another Good Reference: http://jinaldesai.net/linq-to-sql-vs-entity-framework/ (OR)

http://www.slideshare.net/jinaldesailive/linq-to-sql-vs-entity-framework-jinal-desai 3.Difference between LINQ and Stored Procedures S.No 1 LINQ As LINQ is part of .NET, we can use visual studios debugger to debug the queries Stored Procedures It is really very hard to debug the Stored Procedure

With LINQ everything gets With Stored Procedures, we need complied into single DLL hence to provide an additional script for deployment becomes easy. deployment 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! LINQ supports multiple databases LINQ supports abstraction which allows framework to add additional improvements like multi threading. It is much simpler and easier to add this support through LINQ than Stored Procedures. LINQ queries need to compile before execution.Therefore, LINQ is slower than Stored Procedures. Stored Procedures are not type safe i.e., error can be caught only during runtime.

With Stored Procedures which need to be re-written for different databases. As Stored Procedures does not support abstaction , so it is difficult to add additional impovements like multi threading

Stored Procedures are ster thn LINQ because they are precompiled i.e., Stored Procedures have a predictable execution plan.Therefore, if a stored procedure is being executed for the second time, the database gets the cached execution plan to execute the stored procedure.

4.Difference between LINQ to SQL and LINQ to Objects S.No 1 LINQ to SQL LINQ to SQL needs a Data Context object. The Data Context object is the bridge LINQ to Objects LINQ to Objects does not need any intermediate LINQ provider or API.

between LINQ and the database. 2 3 LINQ to SQL returns data of type IQueryable LINQ to SQL is translated to SQL by way of Expression Trees, which allow them to be evaluated as a single unit and translated to the appropriate and optimal SQL statements. LINQ to SQL is translated to SQL calls and executed on the specified database LINQ to Objects returns data of type IEnumerable. LINQ to Objects does not need to be translated.

LINQ to Objects is executed in the local machine memory.

5.Difference between LINQ to SQL and LINQ to Entities S.No 1 2 3 4 5 6 7 8 LINQ to SQL Does not support Conceptual Data Model Does not support Storage Schema Does not support Mapping Schema Does not support New Data Access Provider Does not support Non-SQL Server Database Support Supports Direct Database Connection LINQ to Entities Supports Conceptual Data Model Supports Storage Schema Supports Mapping Schema Supports New Data Access Provider Supports Non-SQL Server Database Support Does not support Direct Database Connection

Does not support Multiple-table Supports Multiple-table Inheritance Inheritance Does not support Single Entity from Multiple Tables Supports Single Entity from Multiple Tables

Reference: http://onlydifferencefaqs.blogspot.in/2012/07/linq-difference-faqs-1.html

Linq difference faqs-2


1.Difference between LINQ to SQL and ADO.NET S.No 1 LINQ to SQL Used for data handling with SQL Server databases only. Uses the extension methods of System.Linq.Queryable class. Introduced in .net Framework 3.0 DataContext is used for Database connectivity. Syntax and coding is somewhat complex. ADO.NET Used for data handling with any database: SQL Server/Access/Oracle/Excel etc. Does not use the extension methods of System.Linq.Queryable class. It is there since the .net Framework 1.0 SqlConnection/OleDbConnection are used for database connectivity Easier syntax and coding.

3 4 5

Note: Both LINQ TO SQL and ADO.NET have connected / disconnected modes of data handling 2.Difference between LINQ and nHibernate S.No 1 2 LINQ LINQ is not an open-source LINQ is an incomplete ORM tool as it needs additional extensions. LINQ is primarily a querying language LINQ is much more useful in small applications where there is no massive dependence on databases. With LINQ, database already exists and the relationships and some programming will be dependent on how the database is defined. nHibernate nHibernate is an open source. nHibernate is an ORM tool

3 4

nHibernate has a limited querying language. nHibernate is much more useful in largel applications where there is massive dependence on databases. With nHibernate, database does not already exist and it needs to be defined .

3.Difference between LINQ and Entity Framework S.No 1 2 3 4 LINQ Used for Rapid Application Development Works with objects in database Mainly woks with SQL Server ".dbml" is created while using LINQ to SQL Entity Framework Used for Enterprise Development Works with Conceptual model of database Works with all data sources ".EDMX" is created while using Entity Framework

Reference: http://onlydifferencefaqs.blogspot.in/2012/07/linq-difference-faqs-2.html

LINQ Difference FAQs-3


1.Difference between First() and Single() extension methods in LINQ S.No 1 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.

2.Difference between FirstOrDefault() and SingleOrDefault() extension method in LINQ S.No 1 FirstOrDefault() It gets the first item that matches a given criteria. SingleOrDefault() If we specify this extension method that means we 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.

3.Difference between Count() and LongCount() extension methods in LINQ S.No 1 Count() Count() has a lesser range than LongCount() long.MinValue = -2,147,483,648 long.MaxValue = 2,147,483,647 LongCount() LongCount() has a greater range than Count(). long.MinValue = -9223372036854775808 long.MaxValue = 9223372036854775807

Its DotNet Framework type is System.Int32

Its DotNet Framework type is System.Int64

Example for Count(): public static long display() { var tempval = (from h in objDB.tbl_mvc_login select h).Count (); return tempval; } Example for LongCount(): public static long display() { var tempval = (from h in objDB.tbl_mvc_login select h).LongCount (); return tempval; } Summary: They both does the same thing .If we want to count something which is quite big then we have to use LongCount() extension method otherwise we can use Count(). Reference: http://onlydifferencefaqs.blogspot.in/2012/08/linq-difference-faqs-3.html

Difference between findall() and where() in LINQ


Difference between findall() and where() in LINQ S.No 1 findall() where()

FindAll() is a function on the Where() works on any type that List type i.e., it can only be implements IEnumerable used on List instances (or instances of classes that inherit from it, of course). It is not a LINQ extension method like Where().So,if our collection is IEnumerable type, then we cannot use FindAll() as it's a function of List. The FindAll method of the List class actually constructs a new list object, and adds results to it. The Where extension method for IEnumerable will simply iterate over an existing list and yield an enumeration of the matching

results without creating or adding anything (other than the enumerator itself.) 3 As findall() creates a list before creating query, hence it is slower than Where() Where is much faster than FindAll. No matter how big the list is, Where takes exactly the same amount of time because Where() just creates a query, It doesn't actually do anything

Suppose we have a class Person : public class Person { public string FirstName { get; set; } public string LastName { get; set; } } Now, create an object of person class : Person person = new Person(); Take a List of person class : List lstPerson = new List(); Now, assign values to this class and add it to list : person.FirstName = "FirstName1"; person.LastName = "LastName1"; lstPerson.Add(person); person.FirstName = "FirstName2"; person.LastName = "LastName2"; lstPerson.Add(person); Now, we want to find all the records where FirstName = "FirstName1". To do this, we have two options : FindAll() and Where() FindAll() : List result = lstPerson.FindAll(delegate(Person per) { return per.FirstName == "Firstname1"; }); Where() : List result = lstPerson.Where((Person per) => per.FirstName == "FirstName1").ToList(); Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-findall-and-wherein.html

Difference between Skip() and SkipWhile() extension methods in LINQ


Difference between Skip() and SkipWhile() extension methods in LINQ S.No 1 Skip() Skip() will take an integer argument and skips the top n numbers from the given IEnumerable SkipWhile() SkipWhile() continues to skip the elements as long as the input condition is true. Once condition turns false it will return all remaining elements.

2.Signature of Skip (): public static IEnumerable Skip( this IEnumerable source, int count); 3.Example: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstNumbersLessThan6 = numbers.Skip(3); In this example, the Skip method will skip the first 3 items from the collection and return the remaining items as an IEnumerable collection. So the output of this example will be 3, 9, 8, 6, 7, 2 and 0. 2a.Signature of SkipWhile (): public static IEnumerable SkipWhile( this IEnumerable source, =unc predicate); 2b.Another signature for overriding: public static IEnumerable SkipWhile( this IEnumerable source, Func predicate); 3a.Example I: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7 }; var remaining = numbers.SkipWhile(n => n < 9); In this example the SkipWhile operator will skip the items from the starting position until the conditions fails. Once the condition has failed then it will return the remaining items as an IEnumerable collection. So the output for this example is 9, 8, 6 and 7. 3b.Example II: int[] numbers = { 5, 4, 1, 3, 9, 8, 2, 0 }; var indexed = numbers.SkipWhile((n, index) => n > index); In this example the SkipWhile method will skip the items greater than their positions.

So in this example the output is 1, 3, 9, 8, 2 and 0. Because the position of the 1 is 2 so here the condition fails. Reference: http://onlydifferencefaqs.blogspot.in/2012/08/difference-between-skip-andskipwhile.html

Take() vs TakeWhile()
Differences between Take() and TakeWhile() extension methods in LINQ S.No 1 Take() Meaning: The Take (int...) method of System.Linq.Queryable class returns the portion of a source i.e. an IEnumerable collection of a source with the first n number of items from the source. Signature of Take (): http://www.csharpcorner.com/UploadFile/d bd951/how-to-usetaketakewhile-andskipskipwhile-inlinq/Images/Linq1.gif TakeWhile() Meaning: The TakeWhile (Func...) will return elements starting from the beginning of an IEnumerable collection until it satisfies the condition specified. If the condition is false then it will return the collection immediately. Signature of TakeWhile (): http://www.csharpcorner.com/UploadFile/dbd95 1/how-to-use-taketakewhile-andskipskipwhile-inlinq/Images/Linq2.gif Another signature for overriding the method: http://www.csharpcorner.com/UploadFile/dbd95 1/how-to-use-taketakewhile-andskipskipwhile-inlinq/Images/Linq3.gif

Example for Take() extension method: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstNumbersLessThan6 = numbers.Take(5); In the above example the Take method returns an IEnumerable collection with only the first 5 items as we have specified in the Take argument. I.e. it will return 5, 4, 1, 3 and 9 only. Examples for TakeWhile() extension method:

Example I: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 9); In the above example the TakeWhile will return the collection of items when the condition fails. i.e. if the control reaches the item 9 then the condition fails here so it will return the items from 5 to 3 [5, 4, 1, 3]. Quick Note: Even though the collection has the items which are less than 9 such as 8,6,7,2,and 0 but it returned only 5,4,1 and 3. The point here is the TakeWhile won't consider the items which are the item which makes the condition fail. Example II: String colection = { "one", "two", "three", "four", "five" }; var strings = collection.TakeWhile(n => n.Length < 4); In this example, the TakeWhile will return the string collection having a length of 4 characters i.e. it will return only the "one" and "two" because the length of the "three" is 5 so it fails there. Example III: String colection = { "one", "two", "three", "four", "five" }; var strings = collection.TakeWhile((s, index)=> s.Length > index ); In this example the TakeWhile will return the items starting from the beginning of the array until a string length is greater than it's position in the collection. So this example will return "one" and "two". Because the "three" has the length of 5 and its position [index] is 2. Since 2 is less than 5 the condition fails at "three". Reference: http://onlydifferencefaqs.blogspot.in/2012/09/take-vs-takewhile.html LINQ vs SQL Keywords Difference between LINQ and SQL Keywords C# LINQ let Count(), Sum(), Skip() VB LINQ Let Aggregate Skip ANSI SQL AS COUNT, SUM, with no group n/a

SkipWhile() Take() TakeWhile() orderby group Distinct()

Skip While Take Take While Order By Group By Distinct

n/a n/a n/a ORDER BY GROUP BY DISTINCT

Note: Select ,From,Where,Join and Into keywords have the same name in C# LINQ,VB LINQ and ANSI SQL. Reference: http://onlydifferencefaqs.blogspot.in/2012/09/linq-vs-sql-keywords.html

You might also like