Stored procedures vs. LINQ queries

.net entity-framework linq linq-to-sql sql-server


What are the advantages and disadvantages of utilizing stored procedures (SQL server 2008) vs. linq queries (combined with an ORM like EF or linq2sql) to query and update a data model? Performance? Speed? Etc...

1/14/2011 2:53:02 AM

Accepted Answer

When you're in the code, Linq is undeniably easier to read. If you don't really go and look at what the "sp GetSomething" sproc performs, seeing a call to execute it doesn't mean anything to you as a developer. observing code

var query = from c in db.TableName
            where c.Name == "foo"
            select c;

You can see precisely what data is being retrieved from it.

On the other hand, if you decide to update the code in stored procedures, you do not need to recompile the program. If you suddenly decide to change awhere " clause or modifyOrder By - Modifying a sproc is simple. It can take longer to change the Linq code.

These are only two that I've seen, but I'm sure there are many more.

1/14/2011 4:34:01 PM

Expert Answer

False dichotomy. According to me, combining raw SQL with tools that merely make it easier will allow you to find the sweet spot of both by employing this crucial talent. The majority of individuals don't require the coupling and deployment concerns that stored procedures create, and ORM performance might be subpar. I'm a huge admirer of parameterized command text since most users won't change RDBMS (and if they do, it will be a massive project anyhow). And tools that help, for example, dapper.

Here's the same thing in dapper as another user's LINQ example of name filtering:

string name = ...
var list = connection.Query<YourType>(
    "select * from TableName where Name=@name",
    new { name }).ToList();

Completely parameterized, highly optimized, secure, fast, tidy, and simple to get right It's done.

8/13/2014 2:03:04 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow