views:

999

answers:

7

Is LINQ a new feature in .NET 4.0? Older versions like .NET 3.5 doesn't have?

I am interested to know about this as it seemed to be somehow related to my the project I am working on.

What is LINQ useful for? It seemed to be able to build Expression Tree. What is an actually Expression Tree? Is LINQ able to extract info like class, method n field from a C# file?

Can someone provide me a working piece of code to demostrate what LINQ can do?

Thank you!

+3  A: 

LINQ is a .NET 3.5 feature with built-in language support from C# 3.0 and Visual Basic 2008. There are plenty of examples on MSDN.

Jeff Yates
LINQ is not only a C# feature. It works with VB as well.
RaYell
Edited accordingly
Jeff Yates
Jeff didn't say LINQ is ONLY a C# feature, just that C#3.0 has LINQ support ;-)
Meta-Knight
+1  A: 

LINQ was introduced with .NET 3.5. This site has a lot of examples.

RaYell
+1  A: 

This is also covered pretty extensively here on SO.

Dan Esparza
A: 

Here is a lot of Linq-examples:
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

JayJay
+7  A: 

Linq was added in .Net 3.5 (and added to the c# 3.0 compiler as well as in slightly limited form to the VB.net compiler in the same release)

In is language integrated query, though it covers many complex additions to both the language and the runtime in order to achieve this which are useful in and of themselves.

The Expression functionality is simply put the ability for a program, at runtime, inspect the abstract syntax of certain code constructs passed around. These are called lambdas. And are, in essence a way of writing anonymous functions more easily whilst making runtime introspection of their structure easier.

The 'SQL like' functionality Linq is most closely associated with (though by no means the only one) is called Linq to Sql where by something like this:

from f in Foo where s.Blah == "wibble" select f.Wobble;

is compiled into a representation of this query, rather than simply code to execute the query. The part that makes it linq to sql is the 'backend' which converts it into sql. For this the expression is translated into sql server statements to execute the query against a linked database with mapping from rows to .net objects and conversion of the c# logic into equivalent where clauses. You could apply exactly the same code if Foo was a collection of plain .net objects (at which point it is "Linq to objects") the conversion of the expression would then be to straight .Net code.

The lambda above written in the language integrated way is actually the equivalent of:

Foo.Where(f => f.Blah == "wibble).Select(f => f.Wobble);

Where Foo is a typed collection. For databases classes are synthesized to represent the values in the database to allow this to both compile, and to allow round tripping values from the sql areas to the .net areas and vice versa.

The critical aspect of the Language Integrated part of Linq is that the resulting language constructs are first class parts of the resulting code. Rather than simply resulting in a function they provide the way the function was constructed (as an expression) so that other aspects of the program can manipulate it.

Consumers of this functionality may simply chose to run it (execute the function which the lambda is compiled to) or to ask for the expression which describes it and then do something different with it.

Many aspects of what makes this possible are placed under the "Linq" banner despite not really being Linq themsleves.
For example anonymous types are required for easy use of projection (choosing a subset of the possible properties) but anonymous types can be used outside of Linq as well.

Linq, especially via the lambdas (which make writing anonymous delegates very lightweight in terms of syntax) has lead to an increase in the functional capabilities of c#. this is reinforced by the extension methods on IEnumerable<T> like Select(), corresponding to map in many function languages and Where() corresponding to filter. Like the anonymous types this is not in and of itself "Linq" though is viewed by many as a strongly beneficial effect on c# development (this is not a universal view but is widely held).

Expressions are a more advanced topic, and understanding of them is entirely unecessary to use linq, though certain 'tricks' are possible using them. In general you would care about Expressions only if you were attempting to write linq providers which is code to take an expression rather than just a function and use that to do something other than what the plain function would do, like talk to an external data source.

Other uses would be when you wish to get some meta data about what the internals of the function is doing, perhaps then compiling the expression (resulting in a delegate which will allow you to execute the expression as a function) and doing something with it or just looking at the metadata of the objects to do reflective code which is compile time verified as this answer shows.

ShuggyCoUk
It's not called "Linq to SQL". What about Linq to objects and Linq to XML. They are all expressed this way.
Jeff Yates
I was referring to it being converted into a sql query behind the scenes rather than just the syntax. I agree it isn#'t sufficiently clear so I will edit accordingly
ShuggyCoUk
+2  A: 

One area of this question that hasn't been covered yet is expression trees. There is a really good article on expression trees (and lambda expression) available here.

The other important thing to bring up about expression trees is that by building an expression tree to define what you are going to do, you don't have to actually do anything. I am referring to deferred execution.

//this code will only build the expression tree
var itemsInStock = from item in warehouse.Items
                   where item.Quantity > 0;

// this code will cause the actual execution
Console.WriteLine("Items in stock: {0}", itemsInStock.Count());
Ty
A: 

System.Linq.Expressions is for hand building (or machine generating) expression trees. I have a feeling that given the complexity of building more complicated functionality that this namespace is under used. However it is exceedingly powerful. For instance one of my co workers recently implemented an expression tree that can auto scale any LINQ to SQL object using a cumultive density function. Every column gets its own tree that gets compiled so its fast. I have been building a specialized compiler that uses them extensively to implement basic functionality as well as glue the rest of the generated code together.

Please see this blog post for more information and ideas.

Steve