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.

Flavors

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.

Resources

86142 questions
709
votes
11 answers

Which method performs better: .Any() vs .Count() > 0?

in the System.Linq namespace, we can now extend our IEnumerable's to have the Any() and Count() extension methods. I was told recently that if i want to check that a collection contains 1 or more items inside it, I should use the .Any() extension…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
667
votes
12 answers

Concat all strings inside a List using LINQ

Is there any easy LINQ expression to concatenate my entire List collection items to a single string with a delimiter character? What if the collection is of custom objects instead of string? Imagine I need to concatenate on object.Name.
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
652
votes
24 answers

LEFT OUTER JOIN in LINQ

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Correct problem: For inner join is easy and I have a solution like this List innerFinal = (from…
Toy
  • 6,573
  • 3
  • 16
  • 4
625
votes
18 answers

Update all objects in a collection using LINQ

Is there a way to do the following using LINQ? foreach (var c in collection) { c.PropertyToSet = value; } To clarify, I want to iterate through each object in a collection and then update a property on each object. My use case is I have a bunch…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
618
votes
17 answers

Is it better to call ToList() or ToArray() in LINQ queries?

I often run into the case where I want to eval a query right where I declare it. This is usually because I need to iterate over it multiple times and it is expensive to compute. For example: string raw = "..."; var lines = (from l in…
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
613
votes
17 answers

LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria

Consider the IEnumerable extension methods SingleOrDefault() and FirstOrDefault() MSDN documents that SingleOrDefault: Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is…
p.campbell
  • 98,673
  • 67
  • 256
  • 322
593
votes
23 answers

Retrieving Property name from lambda expression

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have. eg. GetSortingInfo(u => u.UserId); It worked by casting it as a memberexpression only when the property was a string.…
Schotime
  • 15,707
  • 10
  • 46
  • 75
560
votes
20 answers

How to use LINQ to select object with minimum or maximum property value

I have a Person object with a Nullable DateOfBirth property. Is there a way to use LINQ to query a list of Person objects for the one with the earliest/smallest DateOfBirth value? Here's what I started with: var firstBornDate = People.Min(p =>…
slolife
  • 19,520
  • 20
  • 78
  • 121
556
votes
10 answers

Join/Where with LINQ and Lambda

I'm having trouble with a query written in LINQ and Lambda. So far, I'm getting a lot of errors here's my code: int id = 1; var query = database.Posts.Join(database.Post_Metas, post => database.Posts.Where(x => x.ID…
David
  • 5,579
  • 3
  • 16
  • 4
507
votes
4 answers

LINQ Orderby Descending Query

I have a LINQ query that I want to order by the most recently created date. I tried: var itemList = from t in ctn.Items where !t.Items && t.DeliverySelection orderby t.Delivery.SubmissionDate descending …
109221793
  • 16,477
  • 38
  • 108
  • 160
506
votes
13 answers

C# LINQ find duplicates in List

Using LINQ, from a List, how can I retrieve a list that contains entries repeated more than once and their values?
Mirko Arcese
  • 5,133
  • 2
  • 13
  • 11
479
votes
6 answers

LINQ .Any VS .Exists - What's the difference?

Using LINQ on collections, what is the difference between the following lines of code? if(!coll.Any(i => i.Value)) and if(!coll.Exists(i => i.Value)) Update 1 When I disassemble .Exists it looks like there is no code. Update 2 Anyone know why…
Anthony D
  • 10,877
  • 11
  • 46
  • 67
474
votes
12 answers

What is the difference between IQueryable and IEnumerable?

What is the difference between IQueryable and IEnumerable? See also What's the difference between IQueryable and IEnumerable that overlaps with this question.
Nirmal
462
votes
34 answers

Split List into Sublists with LINQ

Is there any way I can separate a List into several separate lists of SomeObject, using the item index as the delimiter of each split? Let me exemplify: I have a List and I need a List> or List[],…
Felipe Lima
  • 10,530
  • 4
  • 41
  • 39
457
votes
7 answers

How to get index using LINQ?

Given a datasource like that: var c = new Car[] { new Car{ Color="Blue", Price=28000}, new Car{ Color="Red", Price=54000}, new Car{ Color="Pink", Price=9999}, // .. }; How can I find the index of the first car satisfying a certain condition…
codymanix
  • 28,510
  • 21
  • 92
  • 151