tags:

views:

237

answers:

6

It's been some time since Linq to objects has been introduced and i'm curious about it's adoption among developers. From my experience I can't say i have seen a lot of code using it for it's filtering, ordering, and grouping capabilities. Is it the same for you ?

Does developers really find the style of programming it enables more concise and readable than traditional loops?

A: 

IMHO, I find it very useful and I use it whenever I need to query collections, XML, or databases. It has definitely got advantages in terms of ease of use and being able to integrate the ability to query objects using a functional style query language that has quite a lot of power and similarities to SQL, making it easier to learn when you have familiarity with SQL.

I've seen quite a few tutorials about it and I've also seen lots of questions about it on this forum, so I think it's used quite often. It's got the advantage of being able to use it in an object oriented way, especially with databases, where before LINQ one had to write hard coded strings in one's object oriented code.

It's good technology when put to good use!

Tony
+2  A: 

Personally I prefer LINQ to objects over regular loops for most tasks, and looking at our code base I can see, that I am not the only one. However, I am sure that there are many who don't employ LINQ very often.

The main reason I can think of for getting to know LINQ, is to be able to use PLINQ. If you do traditional loops you also have to do all the plumbing to utilize today's multi-core machines yourself. With PLINQ you have to do very little. Mark the query as parallel and the library will try to split the workload on the available cores.

Brian Rasmussen
Yes, I find it maddening now when I'm having to stay compatible with 3.0 and can't drop in a quick Where or Select. It's almost muscle memory now.
itowlson
+2  A: 

I've seen many examples of its use in real production code. I think a lot of people are still confused by the difference between LINQ and LINQ to SQL. I don't see much LINQ to SQL in the real world at all, people use other systems for that. But yes, little inline LINQ snippets are everywhere.

Here's an example of something you might find everywhere, joining the keys of a dictionary into a comma-delimited string of @parameters:

var paramlist = String.Join(",", updatelist.Keys.Select(x => "@" + x).ToArray()) + ",@DateEntered";

Plynx
My experience agrees with yours. I like to wrap up the most common cryptic-looking snippets into their own methods, though. If a developer unfamiliar with LINQ saw that, I think there would be a "WTF" moment.
Stuart Branham
+1  A: 

LINQ is scattered around the newer parts of our 6-ish year old application. If you're using a modern .NET framework, it isn't that difficult to "just use." Granted, the other developers on my team aren't really using it yet, but that may just be unfamiliarity.

The extension methods in System.LINQ really help clean up and make terse a large class of common programming tasks. They're just too handy not to use.

Stuart Branham
+2  A: 

I use LINQ in pretty much all of my work these days. Having the ability to query any enumerable is extremely powerful, especially given the way LINQ, IEnumerable<T>, and IQueryable<T> are designed. The expressive nature of LINQ is not only easy to understand, it can also greatly improve the efficiency of an application by delaying execution until absolutely necessary, and only processing exactly the data that needs to be processed without wastefully creating temporary collections and copying data around.

Beyond the basic IEnumerable and IQueryable stuff, LINQ also introduced the System.Linq.Expressions namespace. By utilizing expressions in your own types and methods, you can inject the benefits of link into your own API's. A lot of things in C# and .NET often require strings that represent physical runtime types. Reflection is most notable, but there are many other instances. Using expressions, you can add compile-time checking of what would otherwise be string data that can't be compile-time checked. This is probably one of the most powerful features that LINQ brought to the table, and one I try to use every time I get the chance. I'm not sure that a lot of people realize they are using LINQ or LINQ-related things in some of the API's I've designed, however it is there.

I think the functional capabilities of LINQ and its close relatives, like expressions and lambdas, are used more than many .NET developers realize. I've heard many developers I work with mention how they dislike LINQ while at the same time use some of its capabilities, such as the Where() or Select() method. It may be that LINQ is sorely misunderstood, and is primarily associated with LINQ to SQL...however LINQ is more than simply a basic ORM framework. It is one of the more powerful and intrinsic capabilities of C# 3.0, and if it isn't used that much, it really should be.

jrista
Can you provide a short example that LINQ makes more efficient via delaying execution?
Stefan Monov
Assume you have a chain of calls, each one makes some modification to a provided collection containing data from a database. Classically, you might select a large data set, and progressively shrink that data set by searching the collection, adding found items to a new collection, and returning, etc. Problem with that is, you query the database initially, and do work at each stage to filter a large result set. With LINQ, you don't actually filter a data set...you simply modify an initial query until you have added all the necessary criteria...THEN execute against the database.
jrista
The difference between the classic approach and the LINQ approach is that classically, you retrieve more data than you need and progressively shrink it. Where as with LINQ, you start with a general query and progressively make it more specific, requesting data only when you actually need it. Granted, you could write a bazillion specific stored procedures against a database to only return exactly what you need for each specific scenario...but then you are creating a whole additional layer of code that needs to be verified and maintained. The LINQ approach allows you to eliminate that work.
jrista
Sorry for so many comments...anyway, this approach need not be limited to databases. It works for any data source. XML documents, Lucene indexes, CSV files, whatever you can think of. Another additional benefit of LINQ...if an exception occurs for whatever reason before you actually need the data...you don't waste time retrieving it. Building a query is cheap, and if some error is encountered part way through its construction, you don't have to worry about wasting network bandwidth or considerable processing resources.
jrista
+2  A: 

I use it also daily, and much more than I expected. Normally there are lots of inelegant for loops in programs for querying things, like:

// check if all are eligible for free shipping    
bool all = true;
for (int i = 0; i < products.Count; ++products)
{
     if (!products[i].FreeShipping)
     {
          all = false;
     }
}

if (all) return;

With linq you write (with no need for comments):

if (products.All(p => p.FreeShipping))
    return;

Without Linq, every dev has his own style of writing these loops. Furthermore, if you write many similar test, it is tempting to "optimize" them by computing multiple values in the same for loop, which makes the code more difficult.

It fits well with my personal goal to always write "The shortest readable program that works".

jdv
nice example, but i don't need to be convinced about Linq usefulness, I actually love it (to the point of abusing it). I want to know if in your entourage people use it and actually find it easier to work with.
Johnny Blaze
They do use it. Overhere only one of the developers was not interested, but otherwise both the seasoned as well as the fresh guys pick it up quickly after seeing a few examples (like I gave). The point of the example is that it kind of sells itself. We are mostly doing Linq to objects, and usually with extension methods, not the sql like syntax.
jdv