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
22
votes
8 answers

Buffering a LINQ query

FINAL EDIT: I've chosen Timothy's answer but if you want a cuter implementation that leverages the C# yield statement check Eamon's answer: https://stackoverflow.com/a/19825659/145757 By default LINQ queries are lazily streamed. ToArray/ToList give…
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
22
votes
2 answers

Group by, Count and Lambda Expression

I am trying to translate the following query: SELECT STATE, COUNT(*) FROM MYTABLE GROUP BY STATE; Into a lambda expression. I am using C# and EntityFramework, however it doesnt seem I can make it work. Here is what I have on my respository so…
N8K8
  • 267
  • 2
  • 4
  • 7
22
votes
4 answers

How do I combine LINQ expressions into one?

I've got a form with multiple fields on it (company name, postcode, etc.) which allows a user to search for companies in a database. If the user enters values in more than one field then I need to search on all of those fields. I am using LINQ to…
gilles27
  • 2,221
  • 6
  • 23
  • 37
22
votes
5 answers

LINQ practice exercises or puzzles?

I'm still trying to learn LINQ, though it's going more smoothly now that I've started to use it daily at work. I still don't feel good at it, though. Does anyone have any challenging practice exercises or puzzles I can use as a code-kata to improve…
Danimal
  • 7,672
  • 8
  • 47
  • 57
22
votes
2 answers

Implement IQueryable wrapper to translate result objects

Update 2013-08-22: After having a look at the 'Building an IQueryable provider series' (thanks for the link!) I got a bit further. I updated the code accordingly. It is still not fully working though. If I understand the tutorial correctly, the…
cyan902
  • 291
  • 1
  • 2
  • 8
22
votes
5 answers

Creating a list filled with new instances of an object

What's the best way to create a list with an arbitrary number of instances of the same object? i.e is there a more compact or efficient way to do the following? static List MyObjs = Enumerable.Range(0, 100) .Select(i => new MyObj()) …
Arithmomaniac
  • 4,604
  • 3
  • 38
  • 58
22
votes
2 answers

VB.NET LINQ Query: Getting The Sum of All Values For A Specific Structure Member

In VB.NET, let's assume I have the following Structure: Public Structure Product Public ItemNo As Int32 Public Description As String Public Cost As Decimal End Structure ... and a Generic List of the Products: Dim ProductsList As New…
ClockEndGooner
  • 672
  • 1
  • 12
  • 24
22
votes
6 answers

Processing a C# Dictionary using LINQ

How do I write the "// Display using Foreach" loop implementation using LINQ Lambda Expression / Statemene Expression? I want to simplify my development and avoid nested foreach loops as much as possible. I am trying to include more logic with in…
Sriram B
  • 651
  • 2
  • 8
  • 20
22
votes
4 answers

What does "Replace with single call to single" mean?

When using LINQ with Single() I always get my line of code underlined in green with the suggestion "Replace with single call to single." What does this mean? Here's an example of a line of code that results in that suggestion: var user =…
Legion
  • 3,922
  • 8
  • 51
  • 95
22
votes
7 answers

When using LINQ, what is the difference between && and multiple where clauses?

I am new to LINQ and discovered yesterday that you can have multiple where clauses such as: var items = from object in objectList where object.value1 < 100 where object.value2 > 10 select object; Or you can write: var items = from object in…
Sci-fi
  • 759
  • 4
  • 8
  • 12
22
votes
5 answers

Difference between Query Expression and Method Expression in LINQ?

I don't know whether the term of above title is appropriate. Just like a and b: var list = Enumerable.Range(0, 100); var a = from l in list where l % 2 == 0 select l; var b = list.Where(l => l % 2 == 0); When should I use each of…
user1031200
22
votes
1 answer

Entity Framework with Linq, inner Join, Group By, Order By

I have a SQL Query select Firma.Name as companyName, Taetigkeit.Taetigkeit as skillName, SUM(Zeit) as time from Zeiterfassung inner join Firma On ZEiterfassung.FirmenID = Firma.ID inner join Taetigkeit on…
Markus_DE_HH
  • 1,061
  • 3
  • 13
  • 29
22
votes
5 answers

Extract the k maximum elements of a list

Let's say I have a collection of some type, e.g. IEnumerable values; Now I need to extract the k highest values from that collection, for some parameter k. This is a very simple way to do this: values.OrderByDescending(x =>…
Henrik Berg
  • 519
  • 5
  • 21
22
votes
4 answers

Using Linq select list inside list

Using LINQ how to select from a List within a List public class Model { public string application { get; set; } public List users { get; set; } } public class Users { public string name { get; set; } public string surname {…
Reno
  • 1,291
  • 4
  • 17
  • 29
22
votes
1 answer

c# / Linq sum where

I have a table NCR containing data of the format: ID | Date | Item | Type | Qty 1 | 01/01/13 | Apple | A | 1 2 | 01/01/13 | Apple | B | 1 3 | 01/01/13 | Orange | C | 1 4 | 01/01/13 | Orange | A | 2 6 | 01/01/13 | Orange…
Gordon Copestake
  • 1,616
  • 4
  • 21
  • 37