Questions tagged [linq]

Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. Please consider using more detailed tags when appropriate, for example [linq-to-sql], [linq-to-entities] / [entity-framework], or [plinq]

This tag is for questions about , a .NET-based DSL (Domain Specific Language), introduced in , for querying data sources such as databases, XML files or in-memory object lists.

Please consider using more detailed tags when appropriate, for example , / , or .

About LINQ

All data sources can be queried using the exact same, readable and easy-to-use syntax - or rather, syntaxes, because LINQ supports two notations:

  • Inline LINQ or query syntax, where queries are expressed in a SQL-like language, with dialects in both C# and VB.NET.

  • Fluent LINQ or query operators, where queries are expressed as lambda expressions and can be linked (LINQed?) using a fluent syntax.

All LINQ query operations consist of three distinct actions:

  1. Obtain the data source.
  2. Create the query.
  3. Execute the query.

Major Implementations:

.NET languages: C#, F#, VB.NET

Some examples:

Fluent syntax (C#)

var result = dbContext.Products
                      .Where(p => p.Category.Name == "Toys" && p.Price >= 2.50)
                      .Select(p => p.Name);

Query syntax (C#)

var result = from product in dbContext.Products
             where product.Category.Name == "Toys"
             where product.Price >= 2.50
             select product.Name;

Query syntax (VB.NET)

Dim result = From product in dbContext.Products _
             Where product.Category.Name = "Toys" _
             Where product.Price >= 2.50 _
             Select product.Name

This query would return the name of all products in the "Toys" category with a price greater than or equal to 2.50.


LINQ comes in many flavors, the most notable are

Other implementations of LINQ can be found on the Internet, such as LINQ to SharePoint, LINQ to Twitter, LINQ to CSV, LINQ to Excel, LINQ to JSON and LINQ to Google.

There are also lots of extensions for LINQ available, which add more operators to the ones .NET offers. A variety of those are open-source projects, for example MoreLINQ.


86142 questions
7 answers

Multiple "order by" in LINQ

I have two tables, movies and categories, and I want to get an ordered list by categoryID first and then by Name. The movie table has three columns ID, Name and CategoryID. The category table has two columns ID and Name. I tried something like the…
  • 20,424
  • 9
  • 40
  • 57
23 answers

LINQ's Distinct() on a particular property

I am playing with LINQ to learn about it, but I can't figure out how to use Distinct when I do not have a simple list (a simple list of integers is pretty easy to do, this is not the question). What I if want to use Distinct on a List on…
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
20 answers

Difference Between Select and SelectMany

I've been searching the difference between Select and SelectMany but I haven't been able to find a suitable answer. I need to learn the difference when using LINQ To SQL but all I've found are standard array examples. Can someone provide a LINQ To…
  • 79,711
  • 83
  • 236
  • 349
10 answers

Group by in LINQ

Let's suppose if we have a class like: class Person { internal int PersonID; internal string car; } I have a list of this class: List persons; And this list can have multiple instances with same PersonIDs, for example:…
  • 13,865
  • 9
  • 28
  • 33
14 answers

Returning IEnumerable vs. IQueryable

What is the difference between returning IQueryable vs. IEnumerable, when should one be preferred over the other? IQueryable custs = from c in db.Customers where c.City == "" select c; IEnumerable custs = from c in…
  • 22,212
  • 29
  • 67
  • 92
14 answers

Group By Multiple Columns

How can I do GroupBy multiple columns in LINQ Something similar to this in SQL: SELECT * FROM GROUP BY , How can I convert this to LINQ: QuantityBreakdown ( MaterialID int, ProductID int, Quantity…
  • 29,307
  • 34
  • 118
  • 188
22 answers

LINQ query on a DataTable

I'm trying to perform a LINQ query on a DataTable object and bizarrely I am finding that performing such queries on DataTables is not straightforward. For example: var results = from myRow in myDataTable where results.Field("RowNo") == 1 select…
  • 25,619
  • 25
  • 85
  • 120
14 answers

When to use .First and when to use .FirstOrDefault with LINQ?

I've searched around and haven't really found a clear answer as to when you'd want to use .First and when you'd want to use .FirstOrDefault with LINQ. When would you want to use .First? Only when you'd want to catch the exception if no results…
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
34 answers

What is the Java equivalent for LINQ?

What is Java equivalent for LINQ?
Ahmed Atia
  • 17,848
  • 25
  • 91
  • 133
22 answers

LINQ equivalent of foreach for IEnumerable

I'd like to do the equivalent of the following in LINQ, but I can't figure out how: IEnumerable items = GetItems(); items.ForEach(i => i.DoStuff()); What is the real syntax?
  • 82,117
  • 31
  • 79
  • 106
11 answers

IEnumerable vs List - What to Use? How do they work?

I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects: List sel = (from animal in Animals join race in Species on animal.SpeciesKey equals race.SpeciesKey …
  • 10,076
  • 6
  • 31
  • 43
12 answers

LINQ Aggregate algorithm explained

This might sound lame, but I have not been able to find a really good explanation of Aggregate. Good means short, descriptive, comprehensive with a small and clear example.
Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
14 answers

Using LINQ to remove elements from a List

Say that I have LINQ query such as: var authors = from x in authorsList where x.firstname == "Bob" select x; Given that authorsList is of type List, how can I delete the Author elements from authorsList that are…
  • 46,577
  • 46
  • 119
  • 147
24 answers

Dynamic LINQ OrderBy on IEnumerable / IQueryable

I found an example in the VS2008 Examples for Dynamic LINQ that allows you to use a SQL-like string (e.g. OrderBy("Name, Age DESC")) for ordering. Unfortunately, the method included only works on IQueryable. Is there any way to get this…
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
11 answers

Use LINQ to get items in one List<>, that are not in another List<>

I would assume there's a simple LINQ query to do this, I'm just not exactly sure how. Given this piece of code: class Program { static void Main(string[] args) { List peopleList1 = new List(); …
  • 12,481
  • 7
  • 30
  • 32
2 3
99 100