131

What is LINQ? I know it's for databases, but what does it do?

informatik01
  • 15,174
  • 9
  • 67
  • 100
Kredns
  • 34,183
  • 49
  • 147
  • 200
  • 23
    I believe I have defeated the "cannot be reasonably answered" criteria. There is no reason this question should be closed. – Amy B Feb 26 '13 at 14:54
  • 2
    [Listen to the creator of LINQ, Erik Meijer, say what it is.](https://www.infoq.com/interviews/erik-meijer-linq) BTW, it is *not* for database alone. – Sнаđошƒаӽ Jul 19 '16 at 10:23

7 Answers7

175

LINQ stands for Language Integrated Query.

Instead of writing YAQL (Yet Another Query Language), Microsoft language developers provided a way to express queries directly in their languages (such as C# and Visual Basic). The techniques for forming these queries do not rely on the implementation details of the thing being queried, so that you can write valid queries against many targets (databases, in-memory objects, XML) with practically no consideration of the underlying way in which the query will be executed.

Let's start this exploration with the parts belonging to the .NET Framework (3.5).

  • LINQ To Objects - examine System.Linq.Enumerable for query methods. These target IEnumerable<T>, allowing any typed loopable collection to be queried in a type-safe manner. These queries rely on compiled .NET methods, not Expressions.

  • LINQ To Anything - examine System.Linq.Queryable for some query methods. These target IQueryable<T>, allowing the construction of Expression Trees that can be translated by the underlying implementation.

  • Expression Trees - examine System.Linq.Expressions namespace. This is code as data. In practice, you should be aware of this stuff, but don't really need to write code against these types. Language features (such as lambda expressions) can allow you to use various short-hands to avoid dealing with these types directly.

  • LINQ To SQL - examine the System.Data.Linq namespace. Especially note the DataContext. This is a DataAccess technology built by the C# team. It just works.

  • LINQ To Entities - examine the System.Data.Objects namespace. Especially note the ObjectContext. This is a DataAccess technology built by the ADO.NET team. It is complex, powerful, and harder to use than LINQ To SQL.

  • LINQ To XML - examine the System.Xml.Linq namespace. Essentially, people weren't satisfied with the stuff in System.Xml. So Microsoft re-wrote it and took advantage of the re-write to introduce some methods that make it easier to use LINQ To Objects against XML.

  • Some nice helper types, such as Func and Action. These types are delegates with Generic Support. Gone are the days of declaring your own custom (and un-interchangable) delegate types.

All of the above is part of the .NET Framework, and available from any .NET language (VB.NET, C#, IronPython, COBOL .NET etc).


Ok, on to language features. I'm going to stick to C#, since that's what I know best. VB.NET also had several similar improvements (and a couple that C# didn't get - XML literals). This is a short and incomplete list.

  • Extension Methods - this allows you to "add" a method to type. The method is really a static method that is passed an instance of the type, and is restricted to the public contract of the type, but it very useful for adding methods to types you don't control (string), or adding (fully implemented) helper methods to interfaces.

  • Query Comprehension Syntax - this allows you to write in a SQL Like structure. All of this stuff gets translated to the methods on System.Linq.Queryable or System.Linq.Enumerable (depending on the Type of myCustomers). It is completely optional and you can use LINQ well without it. One advantage to this style of query declaration is that the range variables are scoped: they do not need to be re-declared for each clause.

    IEnumerable<string> result =
     from c in myCustomers
     where c.Name.StartsWith("B")
     select c.Name;
    
  • Lambda Expressions - This is a shorthand for specifying a method. The C# compiler will translate each into either an anonymous method or a true System.Linq.Expressions.Expression. You really need to understand these to use Linq well. There are three parts: a parameter list, an arrow, and a method body.

    IEnumerable<string> result = myCustomers
     .Where(c => c.Name.StartsWith("B"))
     .Select(c => c.Name);`
    
  • Anonymous Types - Sometimes the compiler has enough information to create a type for you. These types aren't truly anonymous: the compiler names them when it makes them. But those names are made at compile time, which is too late for a developer to use that name at design time.

    myCustomers.Select(c => new 
    {
      Name = c.Name;
      Age = c.Age;
    })
    
  • Implicit Types - Sometimes the compiler has enough information from an initialization that it can figure out the type for you. You can instruct the compiler to do so by using the var keyword. Implicit typing is required to declare variables for Anonymous Types, since programmers may not use the name of an anonymous type.

    // The compiler will determine that names is an IEnumerable<string>
    var names = myCustomers.Select(c => c.Name);
    
informatik01
  • 15,174
  • 9
  • 67
  • 100
Amy B
  • 100,846
  • 20
  • 127
  • 174
14

LINQ (Language INtegrated Query) may refer to:

  • a library for collection and iterator manipulation that makes extensive use of higher-order functions as arguments (System.Linq)

  • a library for passing and manipulation of simple functions as abstract syntax trees (System.Linq.Expressions)

  • a syntax extension to various languages to provide a more SQL-like syntax for processing collections, a more compact notation for anonymous functions, and a mechanism to introduce static helper functions syntactically indistinguishable from final member functions

  • an interface definition to which data providers may conform in order to receive query structure and potentially perform optimization thereon, or occasionally the compatible data providers themselves

The components may be used in isolation or combined.

Jeffrey Hantin
  • 33,679
  • 7
  • 71
  • 92
11

In a nutshell, LINQ (Language-Integrated Query) allows you to write queries directly in your code. Those queries can be on relational databases, but also on XML or in-memory container objects, such as arrays and lists. More information is available in MSDN library: http://msdn.microsoft.com/en-us/library/bb308959.aspx

Vojislav Stojkovic
  • 7,735
  • 4
  • 33
  • 47
8

I'm gonna try for a simple answer: LINQ is a way for you to query your database (or other datastore, XML etc) using a query language that is similar to SQL but can be compiled inside a .NET application.

jcollum
  • 36,681
  • 46
  • 162
  • 279
  • 1
    So what exactly is the difference between Linq and SQL? – Kredns Jan 23 '09 at 01:27
  • There's a more sophisticated name for this I think but the structures differ: SQL is Select From Where and LINQ is From Where Select. LINQ is easier to loop with. LINQ is just easier :) and the SQL it produces is usually good enough considering the time savings. – jcollum Jan 23 '09 at 01:45
  • 2
    to be honest, one could argue SQL *should* be written as `From Where Select, etc.` - i.e. it should be written in the way that the Result Set is actually formed – Don Cheadle Jun 24 '15 at 13:56
6

LINQ stands for Language Integrated Query, and is a way of providing a general purpose "querying" mechanism in the CLR.

At it's most basic level, this consists of a set of methods on IEnumerable<T> - eg., Select, Sum, Where - that can be used for restrictions, projections, etc.[1]

To take it a bit further, LINQ also defines a new LINQ provider model that can take an expression tree and use it to run "native" queries against a datasource outside of the CLR - eg., LINQ to SQL, LINQ to XML, LINQ to NHibernate, etc.

C# and VB.NET have also defined a query syntax that allows you to write strongly typed queries inline (that looks very similar to SQL), which the compiler then translates into the equivalent IEnumerable<T> calls.

To me, the most interesting thing about LINQ is all of the C# and VB.NET features that were needed to support it are useful in their own right. Extension methods, anonymous types, lambda expressions, and implicit typing were all required to support LINQ - but we tend to use those features outside of a pure LINQ context.

[1] Those are relational terms, functional programmers would probably prefer Map, Reduce, Fold, etc.

Mark Brackett
  • 81,638
  • 17
  • 102
  • 150
6

LINQ is a technology for extracting data using an idiom derived from the C# programming language. While it owes much in functional design to SQL, it is fundamentally its own data querying language. It operates across a broad spectrum of data sources (SQL databases, in-memory representations, XML, etc.). LINQ-To-SQL, in particular, should be seen as a contrast to the traditional use of embedded SQL which suffers from what is often referred to as an "impedance mismatch" between the SQL programming and C#/VB programming.

For a discussion of LINQ and its limitations, you may want to take a look at this related question: Doesn’t LINQ to SQL miss the point?

Community
  • 1
  • 1
Mark Brittingham
  • 27,794
  • 11
  • 75
  • 106
0

http://msdn.microsoft.com/en-us/netframework/aa904594.aspx

"The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities."

Jobo
  • 960
  • 6
  • 11
  • So is it for databases? For what I've seen here ( in SO ) is more like for collections in the .net environment. – OscarRyz Jan 23 '09 at 01:21