LINQ - advantage - Asked By Lavinia Rayan on 09-Jan-12 11:14 PM


Hi,
what are the advantages in Linq?what are the situations we ll used Linq?please explained?
smr replied to Lavinia Rayan on 09-Jan-12 11:31 PM
hi

LINQ may be used to access all types of data, whereas embedded SQL is limited to addressing only databases that can handle SQL queries.

Below are the advantages and Disadvantages of LINQ


Advantages:
  1.  It is a cleaner and typesafety.
  2. It is checked by the compiler instead of at runtime
  3. It can be debugged easily.
  4.  It can be used against any datatype - it isn't limited to relational databases, you can also use it against XML, regular objects.
  5. It can able to query not just tables in a relational database but also text files and XML files.

Disadvantages:

  1. LINQ sends the entire query to the DB hence takes much network traffic but the stored procedures sends only argument and the stored procedure name. It will become really bad if the queries are very complex. 
  2. Preformance is degraded if we don't write the linq query correctly.
  3. If you need to make changes to the way you do data access, you need to recompile, version, and redeploy your assembly.
Jitendra Faye replied to Lavinia Rayan on 09-Jan-12 11:36 PM

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. MSDN has a popular set of samples that demonstrate various aspects of LINQ, samples for both Visual C# and Visual Basic. These cover a range of areas, including restriction operators, grouping operators, aggregate operators, and a lot more.


Follow this link , here you will get basic examples-

http://msdn.microsoft.com/en-us/vstudio/aa336746


Advatages-

  • No magic strings, like you have in SQL queries
  • Intellisense
  • Compile check when database changes
  • Faster development
  • Unit of work pattern (context)
  • Auto-generated domain objects that are usable small projects
  • Lazy loading.
  • Learning to write linq queries/lambdas is a must learn for .NET developers.
Riley K replied to Lavinia Rayan on 09-Jan-12 11:44 PM
LINQ offers a great way to query database model where the whole logical model of the database is generated in the form of entities

You can also query on collections

Suppose to select all rows from table we use Select * from Emp

In LINQ we car assign the results to a Var type or List


  Var results=From n in dc.Employee
                        select n;



to query two tables, an example we can use is using the Northwind database.  Let's assume we want to join the Customers and Orders tables together

We would write a query like this to retrieve the OrderID, and OrderDate from the Order table, and the CustomerID and ContactName from the Customer table:

class Program
  {
  static void Main(string[] args)
  {
  using (DataClasses1DataContext db = new DataClasses1DataContext())
  {
 
 
  var query_results = from d in db.Customers
      join o in db.Orders
      on d.CustomerID equals o.CustomerID
      select new { o.OrderID, o.OrderDate, d.CustomerID, d.ContactName };
    
 
 
  }
  }
   
 
 
}


 You can refer 101 LINQ samples
code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Regards

Chintan Vaghela replied to Lavinia Rayan on 10-Jan-12 12:06 AM

Hello,

 

It's very obvious that linq is a special feature and reduces a lot of works.

But large sets of data in the linq can generate some performance issues. However microsoft provides remedy for that. Check the following link.

http://msdn.microsoft.com/en-us/magazine/cc721610.aspx

Mark this as answer, if it is.....

 

 

Hope this is helpful !

Thanks