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
21
votes
5 answers

Linq to SQL: execution order when calling SubmitChanges()

I have 2 related database tables which in simplified form look like this Product( product_id, name ) ProductSpecs( spec_id, product_id, name, value ) Foreign key is set via product_id field and ProductSpecs table has a unique…
Alan Mendelevich
  • 3,591
  • 4
  • 32
  • 50
21
votes
1 answer

How to use .Where in generic list

I have a List(Of MyType) and I would like to use LINQ to get a subset of the list. On MyType there's a field called AccountNumber. Can I use LINQ to say soemthing like this? Dim t As List(Of MyType) t = GetMyTypes() t = t.Where(AccountNumber =…
Tigran
  • 872
  • 1
  • 9
  • 25
21
votes
2 answers

ef-core load collection property of nested tph inherited member

Given the following class structure public class Parent { public Guid Id { get; public List Children { get; set; } } public abstract class BaseChild { public int Id { get; set; } public…
Elmer Ortega
  • 469
  • 4
  • 12
21
votes
2 answers

Where to call .AsParallel() in a LINQ query

The question In a LINQ query I can correctly (as in: the compiler won't complain) call .AsParallel() like this: (from l in list.AsParallel() where select l).ToList(); or like this: (from l in list where select…
user7061022
21
votes
2 answers

LINQ- Combine Multiple List and order by a value (.Net 3.5)

I'm trying to combine a number of List where T:IGetTime (i.e T will always have method getTime()). Then I'm trying order the items by the DateTime that getTime() returns. My LINQ looks like this: public static List Combine(List one,…
Roberto Bonini
  • 7,164
  • 6
  • 39
  • 47
21
votes
4 answers

Using LINQ, how do I find an object with a given property value from a List?

I have a class called Questions. This Questions has properties QuestionID and QuestionAnswer. While iterating through this List of Question in foreach, I have to find .QuestionID = 12. If I find .QuestionID = 12 then I have to immediately assign a…
James123
  • 11,184
  • 66
  • 189
  • 343
21
votes
2 answers

Convert DataRow to Dictionary using LINQ

I need to convert DataRow into Dictionary using LINQ. The code below will get the DataRow, the next step is I need convert it to dictionary(ColumnName, RowVale) var WorkWeekData = from data in mWorkWeekData.AsEnumerable () where…
QKWS
  • 1,069
  • 9
  • 22
  • 41
21
votes
2 answers

NHibernate query runs only once, then throws InvalidCastException

I have a simple query like below: var employeeTeam = Session.Query() .Where(x => x.StartEffective <= competency.FinalDate && // competency.FinalDate is a DateTime …
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
21
votes
6 answers

LINQ Skip still enumerates skipped items

In the following test: int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Func boom = x => { Console.WriteLine(x); return x; }; var res = data.Select(boom).Skip(3).Take(4).ToList(); Console.WriteLine(); res.Select(boom).ToList(); The result…
ericosg
  • 4,926
  • 4
  • 37
  • 59
21
votes
6 answers

Linq to XML - update/alter the nodes of an XML Document

I've got 2 Questions: 1. I've sarted working around with Linq to XML and i'm wondering if it is possible to change an XML document via Linq. I mean, is there someting like XDocument xmlDoc = XDocument.Load("sample.xml"); update item in…
knox
21
votes
7 answers

C# Enumerable.Take with default value

What is the best way to get exactly x values from an Enumerable in C#. If i use Enumerable .Take() like this: var myList = Enumerable.Range(0,10); var result = myList.Take(20); The result will only have 10 elements. I want to fill the missing…
HectorLector
  • 1,851
  • 1
  • 23
  • 33
21
votes
4 answers

Is there a pattern using Linq to dynamically create a filter?

Is there a pattern using Linq to dynamically create a filter? I have the need to create custom filtering on a list, in the past I would just dynamically create the SQL...it doesn't seem like this is possible with Linq.
emcpadden
  • 1,461
  • 2
  • 14
  • 15
21
votes
10 answers

How to update value in a List using LINQ

I have a list which I want to update using LINQ. class Student { private string name; private int marks; public string Name { get; set;} public int Marks { get; set; } public Student(string name, int marks) { Name =…
user3625024
  • 319
  • 1
  • 2
  • 11
21
votes
4 answers

Using LINQ to generate prime numbers

Following is an interview question: The following one-liner generates and displays the list of first 500 prime numbers. How would you optimize it using parallel LINQ while still keeping it a SINGLE C# STATEMENT: MessageBox.Show(string.Join(",", …
dotNET
  • 33,414
  • 24
  • 162
  • 251
21
votes
7 answers

LINQ Next Item in List

Taking a look at my question HERE, I now want to return the next recommendation object (after) the one that matches the criteria. So say I found item 6 out of 10, I'd like the query to return item 7 instead. Or is there a better way?
griegs
  • 22,624
  • 33
  • 128
  • 205
1 2 3
99
100