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
6 answers

LINQ Performance for Large Collections

I have a large collection of strings (up to 1M) alphabetically sorted. I have experimented with LINQ queries against this collection using HashSet, SortedDictionary, and Dictionary. I am static caching the collection, it's up to 50MB in size, and…
Peter J
  • 57,680
  • 8
  • 38
  • 45
21
votes
2 answers

How would you write an Upsert for LINQ to SQL?

So I'd like to write a generic Upsert function for LINQ to SQL and I'm having some trouble conceptualizing how to do it. I'd like it to work something like this: var db = new DataContext(); db.Customers.UpsertOnSubmit(customer); So it would have…
JC Grubbs
  • 39,191
  • 28
  • 66
  • 75
21
votes
2 answers

Joining three tables and using a left outer join

I have three tables. Two of them join equally but one will need to join with a left. I'm finding a lot of code to do this in linq but between two tables only. Here is the SQL code that I'm trying to re-code within LINQ. SELECT PRSN.NAME …
MdeVera
  • 647
  • 5
  • 8
  • 22
21
votes
1 answer

C# About IEnumerable.Aggregate

I did some tests about IList.Aggregate(), but the answer does not make sense to me. List Data1 = new List { 1,0,0,0,0}; var result = Data1.Aggregate((total, next) => total + total); The result is 16. I expected it to be 32. Can…
retide
  • 1,170
  • 1
  • 13
  • 31
21
votes
3 answers

Faster alternatives to .Distinct()

I'm making a video game where performance is critical. I'm using the .Distinct() extension method to get unique value from a List. Is there a faster way to do so? (even if it means having many more lines of code)
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
21
votes
5 answers

What are the advantages of LINQ to SQL?

I've just started using LINQ to SQL on a mid-sized project, and would like to increase my understanding of what advantages L2S offers. One disadvantage I see is that it adds another layer of code, and my understanding is that it has slower…
alchemical
  • 13,559
  • 23
  • 83
  • 110
21
votes
1 answer

Convert String to Int in EF 4.0

Is there any way of doing this at all? I have a string field in the DB and I want to parse it into a int property in my LINQ query (yes, it must be at the IQueryable level, not in memory). I know 2 years ago EF 1.0 couldn't do this (even though LINQ…
Jeff
  • 35,755
  • 15
  • 108
  • 220
21
votes
3 answers

Type system oddity: Enumerable.Cast()

Consider: enum Foo { Bar, Quux, } void Main() { var enumValues = new[] { Foo.Bar, Foo.Quux, }; Console.WriteLine(enumValues.GetType()); // output: Foo[] Console.WriteLine(enumValues.First().GetType()); // output: Foo …
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
21
votes
5 answers

C# Is there a LINQ to HTML, or some other good .Net HTML manipulation API?

I have a C# WPF application that needs to consume data that is exposed on a webpage as a HTML table. After getting inspiration from this url I tried using Linq to Xml to parse the Html document, but this only works if the HTML document is extremely…
Doctor Jones
  • 21,196
  • 13
  • 77
  • 99
21
votes
3 answers

Who has bought the autocompletion for linqpad?

Who has bought the autcompletion feature for Linqpad ? I know it's only $ 19 but I'd like to hear from you if it's worth it... Does it have any bugs? Does it really help in speeding up your linq queries development? Is there any limitations or any…
Andre Gallo
  • 2,261
  • 5
  • 23
  • 46
21
votes
2 answers

Linq group by query

Trying to work out a linq query and was wondering if you guys could help. I have a list of objects foo and each foo object has a list of bar. bar has an active date and a numeric value. for example foo bar -> 01/02/05, 10000 bar -> 04/06/10,…
mat-mcloughlin
  • 6,492
  • 12
  • 45
  • 62
21
votes
2 answers

How much database performance overhead when using LINQ?

How much database performance overhead is involved with using C# and LINQ compared to custom optimized queries loaded with mostly low-level C, both with a SQL Server 2008 backend? I'm specifically thinking here of a case where you have a fairly…
Fred
  • 2,713
  • 3
  • 27
  • 30
21
votes
4 answers

Use LINQ to group a sequence of numbers with no gaps

With this array int[]{ 1, 2, 3, 4, 7, 8, 11, 15,16,17,18 }; How can i convert to this string array "1-4","7-8","11","15-18" Suggestions ? Linq ?
Alexandre
  • 7,004
  • 5
  • 54
  • 72
21
votes
5 answers

How to apply multiple orderby in linq query

I want to apply order by on multiple columns some ascending and others are in descending order in LINQ. How can i do that?
Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132
21
votes
6 answers

how do access previous item in list using linQ?

I have: List A = new List(){1,2,3,4,5,6}; List m=new List(); for(int i=1;i
ratty
  • 13,216
  • 29
  • 75
  • 108