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
23
votes
3 answers

How to Left Outer Join two DataTables in c#?

How can I Left Outer Join two data tables with the following tables and conditions while keeping all columns from both tables? dtblLeft: id col1 anotherColumn2 1 1 any2 2 1 any2 3 2 any2 4 3 any2 5 3 …
Soenhay
  • 3,958
  • 5
  • 34
  • 60
23
votes
4 answers

LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains operator

Consider this LINQ To SQL query. It's intention is to take a string[] of search terms and apply the terms to a bunch of different fields on the SQL table: string[] searchTerms = new string[] {"hello","world","foo"}; List = db.Custs.Where(c =>…
p.campbell
  • 98,673
  • 67
  • 256
  • 322
23
votes
4 answers

NHibernate.Linq LIKE

How can I produce this query using NHibernate.Linq? WHERE this_.Name LIKE @p0; @p0 = 'test' // Notice NO % wild card Note, this is not Linq To Sql or Entity Framework. This is NHibernate. Edit: Here is the desired query using…
mxmissile
  • 11,464
  • 3
  • 53
  • 79
23
votes
2 answers

How to query an XDocument with LINQ when elements have a colon in their name?

I am trying to use LINQ to XML in an with the XDocument object. How do you query the result element in the example below? SUCCESS PRIMARY
Jim
  • 233
  • 1
  • 2
  • 5
23
votes
6 answers

Linq with alias

I have the following line in c#: var name = (from x in db.authors where fullName == "Jean Paul Olvera" orderby x.surname select new { x.id_author, fullName= String.Concat(x.name," ",…
Jean Paul Olvera
  • 379
  • 1
  • 4
  • 9
23
votes
2 answers

LINQ expressions. Variable 'p' of type referenced from scope, but it is not defined

I'm building a LINQ query dynamically with this code. It seems to work, but when i have more than one searchString in my search, (so when multiple expressions are added, i get the following error: Variable 'p' of type referenced from scope, but it…
Tys
  • 3,592
  • 9
  • 49
  • 71
23
votes
4 answers

How can I convert Linq results to DTO class object without iteration

I'm building a Web API project that will be made available to third-party's and also used by my own web application/s. The Web API methods will return JSON representations of complex types/objects. These are pre-defined classes which I can make…
Dan
  • 519
  • 1
  • 4
  • 10
23
votes
1 answer

Linq ToList/ToArray/ToDictionary performance

Well I encounter many situations where having an IEnumerable is not enough. However I'm unsure about the performance of the above method calls. What I really want to ask is: Is the performance of ToList/ToArray: an O(n) operation which copies the…
Jonny
  • 2,787
  • 10
  • 40
  • 62
23
votes
8 answers

C#: Remove duplicate values from dictionary?

How can I create a dictionary with no duplicate values from a dictionary that may have duplicate values? IDictionary myDict = new Dictionary(); myDict.Add("1", "blue"); myDict.Add("2", "blue"); myDict.Add("3",…
User
  • 62,498
  • 72
  • 186
  • 247
23
votes
4 answers

How to group items by index? C# LINQ

Suppose I have var input = new int[] { 0, 1, 2, 3, 4, 5 }; How do I get them grouped into pairs? var output = new int[][] { new int[] { 0, 1 }, new int[] { 2, 3 }, new int[] { 4, 5 } }; Preferably using LINQ
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
23
votes
4 answers

Linq syntax for OrderBy with custom Comparer

There are two formats for any given Linq expression with a custom sort comparer: Format 1 var query = source .Select(x => new { x.someProperty, x.otherProperty } ) .OrderBy(x => x, new myComparer()); Format 2 var query = from x in…
Steve Konves
  • 2,648
  • 3
  • 25
  • 44
23
votes
6 answers

Do LINQ queries have a lot of overhead?

Are simple LINQ queries on an IEnumerable lightweight or heavyweight? How do they compare to writing for or foreach loops by hand? Is there a general guideline for when to prefer LINQ versus manual searching? For example: var lowNums = from n…
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243
23
votes
2 answers

Convert or map a list of class to another list of class by using Lambda or LINQ?

The question and answer of converting a class to another list of class is cool. How about to convert a list of MyData to another list of MyData2? For example: List list1 = new List(); // somewhere list1 is populated List
David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
23
votes
2 answers

How do I group data in an ASP.NET MVC View?

In reporting tools like Crystal Reports, there are ways to take denormalized data and group it by a particular column in the data, creating row headings for each unique item in the specified column. If I have this: Category1 Data1 Category1 …
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
23
votes
4 answers

Entity framework where, order and group

I'm using the following LINQ to select data from a table: (from m in entity.Results where m.Group == 0 || m.Group == 1 orderby m.Points descending select m); This gives me a result of all Users who are in Group 1 or 2. With that i can display the…
Vivendi
  • 20,047
  • 25
  • 121
  • 196