tags:

views:

823

answers:

12

Are there any good Reasons not to use LINQ in my projcts? We use .Net 3.5 and VSTO 3.0.

+9  A: 

Personally I can't see why you wouldn't want to use it. It makes code so much more readable. IMO the only reason you wouldn't want to use it is if you knew there was a more efficient way of performing a task outwith LINQ.

James
A: 

Well, if you want to make a step backwards and work 2xtimes longer on your projects you shouldn't use it ;-)

I think there is no real good reason to NOT use it. You are faster, smarter and you can make less mistakes and have more control since you can work "datatyped"

k0ni
+1  A: 

Please have a look at this topic : it may help you to have an answer.

controlbreak
A: 

In a very short answer: no, there is no reason not to use Linq. Although it can be slightly hard to grasp for people new to the concept of functional programming (but only slightly), once you get the hang of it, you will love it, and it can in general greatly improve the readability of the code.

Razzie
A: 

Yes, definitely makes things more readable and concise. Certainly did for the 15 years I used it in Visual FoxPro ;)

Alan B
LINQ has been around that long?
Kezzer
From the wikipedia entry "_LINQ was released as a part of .NET Framework 3.5 on November 19, 2007._". Link here: http://en.wikipedia.org/wiki/Language_Integrated_Query
Kezzer
I think this answer is meant to (somewhat humorously) point to the fact that VFP had LINQ-ish functionality, with in-memory cursors and SQL support directly in the language, for years.
Eyvind
+1  A: 

It depends on whether you're referring to LINQ to SQL or LINQ as a modelling tool. Personally I use LINQ for modelling only because my superiors who are actually less educated than me in this area refuse to it for a number of reasons. One reason is that they've stopped adding new features completely and are only maintaining bug fixes in LINQ now. Secondly is that it's supposedly slower than direct SQL, which to a certain degree is true when running lower end hardware perhaps, I don't know. Thirdly is a domain perspective, if you want to shy away from SQL injection vulnerabilities supposedly it's wise to use stored procedures instead. In all instances for us, we use LINQ for modelling only now, stored procedures are "compiled" on the server after being run for the first time.

Personally I just meet requirements, I have no powers for decision, but LINQ is great for modelling and includes many handy features.

Kezzer
You don't have to use stored procedures to avoid SQL injection, using sql parameters should be enough to protect from this.
James
Such as using {0} for paramaterized queries?
Kezzer
no like "SELECT * FROM ATable WHERE AField=@value" and using an SQL Parameter to define @value.
James
So doing `myDataContext.ExecuteQuery<MyObj>("SELECT * FROM tbl WHERE field = {0}", id);` is bad? How, in the context of `DataContext`, can you use parameterized queries as you've defined?
Kezzer
No its not bad, this is essentially what LINQ does behind the scenes anyways. I wasn't refering to using LINQ I was refering to using direct SQL with SQL command.
James
Ah okay, that's handy to know, I wasn't aware at all and was wanting to know about that.
Kezzer
Oh also, have you got a reference specifically to SQL injection and the use of queries in the context that I gave? I need it for an evidence report at work ;)
Kezzer
Well in the context you have, as I said before, this is essentially what LINQ is generating in the background. This article might help you http://thedatafarm.com/blog/data-access/linq-to-entities-entity-sql-parameterized-store-queries-and-sql-injection/
James
+2  A: 

The only reason would be if you have a very performance critical app as LINQ to Objects queries usually perform worse than for loops. This will change with .Net 4.0, when you can make use of PLINQ's parallel processing features.

Anyhow, if you have such a performance critical app, you probably should not use a managed environment anyways. in 99% of all cases LINQ will make your code more readable and more elegant. It's deferred execution mechanism may even gain you some performance under the right circumstances.

Manu
+2  A: 

The LINQ architecture is good - but some may prefer to read source code that does not use the LINQ SQL-like syntax. So if you're strict on readability, you may want not to use that syntax.

Lars D
Readability is not that big issue.
crauscher
A: 

not to use LINQ if you mean LINQ as "LINQ to SQL" and your SQL Server has performance problem with a Adhoc Query Execution.

kazuk
+5  A: 

The other answers have implied this, but here it is explicitly:

The term "LINQ" can refer to the overall set of technologies for Language INtegrated Query. These technologies permit a LINQ Query to be turned into an expression tree. That tree can be executed at a later time. By a "LINQ Provider".

The LINQ Provider looks at the expression tree and converts it into, for instance, SQL statements, or XML DOM navigation, or navigating through an object graph. These providers have names like "LINQ to SQL", "LINQ to Entities", "LINQ to Objects", etc.

There could be reasons to not use a specific provider.


Many of the individual LINQ providers come with additional tooling (some would say, "baggage"). This tooling helps provide some of the most common "pros and cons" about LINQ.

In particular, LINQ to SQL provides a direct, one to one mapping to the database schema. Each LINQ class corresponds to a single database table. If the structure of the database changes, then so does the code. This can be mitigated to an extent by using views.

I believe that LINQ to SQL is limited to Microsoft SQL Server. Also, Microsoft has stated that LINQ to SQL will not be a priority investment going forward.

LINQ to Entities is part of the ADO.NET Entity Framework (EF). EF provides for modeling your entities in a more abstract, conceptual level. For instance, you might have a Person entity that takes data from two tables that have a one to one mapping. Code accessing this entity need not care how the tables are broken out in the database.

I believe that LINQ to Entities is meant to work with many ADO.NET providers, not just SQL Server. Also, this is where Microsoft says it will be placing its investments going forward.


In both of these cases of LINQ to "some database", it's almost always going to be the case that a experienced SQL developer and/or DBA can write better queries than will be generated by LINQ. If performance is the top requirement, I believe that LINQ to Entities can map stored procedures into methods on the entities.

The big benefit of these LINQ providers is that you don't need that experienced SQL developer or DBA when you're working in an environment where performance is not the top priority. This frees them up to optimize queries that actually need their expertise.

Another reason not to use these directly is if you have security considerations preventing your users from having SELECT permissions to the database tables. In that case, use views or stored procedures.

John Saunders
+1  A: 

Yes, there are reasons. I'm going to talk about some of the difficulties I've faced when using various LINQ providers for RDBMS.

LINQ works great in most cases, but when you want to build complex queries (e.g. in reporting apps) it's statically-typed nature becomes a disadvantage. It's hard to, for instance, conditionally JOIN, or conditionally GROUP BY, because the result type changes, even if in the end you want to project the same fields. It's also hard to do LEFT JOIN. If you really appreciate dynamic queries then SQL and ESQL are better options.

Also, the same LINQ query can be (and usually is) interpreted differently depending on the provider, which can lead to unexpected results when switching providers.

About SQL functions, not all the functions you need are mapped to CLR methods, and not all providers offer a extensibility mechanism for mapping functions.

Finally, the performance issue. Converting expression trees to store queries can be expensive, and sometimes the end result is not the best possible query. The most elegant source code is not always the best runtime code.

Max Toro
A: 

I have encountered some issues with it that I've had to work around. For example, depending on how your database relations are setup you may find that linq2sql works great for querying, but has trouble updating correctly. This happened to me and what I did was setup two sets of linq objects. The first had all the relationships I needed so I could use simpler syntax for querying, but the second didn't contain the relationships for when I wanted to update since I didn't need them then.

Performance shouldn't be a reason not to use it because generally that's only an issue for certain processes. And it's not like you have to use linq exclusively or not at all. I think a lot of people think "oh, direct sql is faster and there are some things I need to do that HAVE to be fast so I can't use linq". That's just silly though. Use it where you can, and use direct sql where you can't.

Brandon