Friday, March 29, 2013

LINQ Examples


Check it out http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
http://weblogs.asp.net/scottgu/archive/2006/05/14/446412.aspx

LINQ Advantage:

  1. Abstraction: This is especially true with LINQ-to-Entities. This abstraction also allows the framework to add additional improvements that you can easily take advantage of. PLINQ is an example of adding multi-threading support to LINQ. Code changes are minimal to add this support. It would be MUCH harder to do this data access code that simply calls sprocs.
  2. Debugging support: I can use any .NET debugger to debug the queries. With sprocs, you cannot easily debug the SQL and that experience is largely tied to your database vendor (MS SQL Server provides a query analyzer, but often that isn't enough).
  3. Vendor agnostic: LINQ works with lots of databases and the number of supported databases will only increase. Sprocs are not always portable between databases, either because of varying syntax or feature support (if the database supports sprocs at all).
Summary:
1. when talking single DB table and small set of data CRUD, LINQ is as fast as SP. But for much more complicated logic, stored procedure is more performance tweakable.
2. Easier to port to another DB - no procs to port.

LINQ Disadvantage:

  1. Network traffic: sprocs need only serialize sproc-name and argument data over the wire while LINQ sends the entire query. This can get really bad if the queries are very complex. However, LINQ's abstraction allows Microsoft to improve this over time.
  2. Recompiling: If you need to make changes to the way you do data access, you need to recompile, version, and redeploy your assembly. Sprocs can sometimes allow a DBA to tune the data access routine without a need to redeploy anything.
Summary:
  • Store Procedure Prevent reverse engineering (if created With Encryption, of course)
  • Store Procedure Better centralization of database access
  • Store Procedure Ability to change data model transparently (without having to deploy new clients); especially handy if multiple programs access the same data model

In short:

Both LINQ and SQL have their places. Both have their disadvantages and advantages.
Sometimes for complex data retrieval you might need stored procs. And sometimes you may want other people to use your stored proc in Sql Server Management Studio.
Linq to Entities is great for fast CRUD development.
Sure you can build an app using only one or the other. Or you can mix it up. It all comes down to your requirements. But SQL stored procs will no go away any time soon.

Note: 1) LINQ works with lots of databases " But LINQTOSQL only supports SQL Server.

No comments:

Post a Comment