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

Convert List> into List in C#

I have a List>. I would like to convert it into a List where each int is unique. I was wondering if anyone had an elegant solution to this using LINQ. I would like to be able to use the Union method but it creates a new List<>…
user57230
  • 233
  • 1
  • 2
  • 5
23
votes
7 answers

How can I implement NotOfType in LINQ that has a nice calling syntax?

I'm trying to come up with an implementation for NotOfType, which has a readable call syntax. NotOfType should be the complement to OfType and would consequently yield all elements that are not of type T My goal was to implement a method which…
Christoffer Lette
  • 14,346
  • 7
  • 50
  • 58
23
votes
3 answers

Select single column from dataset with LINQ

Just getting my head around all this LINQ stuff and it seems I'm stuck at the first hurdle. I have a datatable as such: OrderNo LetterGroup Filepath ----------- ----------- -------------------------------------------------- 0 0 …
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
23
votes
5 answers

How does IEnumerable.ToArray() work?

Is it a two-pass algorithm? i.e., it iterates the enumerable once to count the number of elements so that it can allocate the array, and then pass again to insert them? Does it loop once, and keep resizing the array? Or does it use an intermediate…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
23
votes
7 answers

Is there a C# unit test framework that supports arbitrary expressions rather than a limited set of adhoc methods?

Basically NUnit, xUnit, MbUnit, MsTest and the like have methods similar to the following: Assert.IsGreater(a,b) //or, a little more discoverable Assert.That(a, Is.GreaterThan(b)) However, there are a limited number of such comparison operators…
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
23
votes
5 answers

Entity Framework with LINQ aggregate to concatenate string?

This is easy for me to perform in TSQL, but I'm just sitting here banging my head against the desk trying to get it to work in EF4! I have a table, lets call it TestData. It has fields, say: DataTypeID, Name, DataValue. DataTypeID, Name,…
Phil Figgins
  • 796
  • 1
  • 8
  • 22
23
votes
8 answers

C# LINQ select from where value is not contained in array / list

New to LINQ and not sure on the correct syntax for what I want to do. I have a "Blocklist", an array or list (could be either) of bad codes that I don't want put into this new "keys" list that I am making currently... var keys = (from s in…
user5850963
23
votes
3 answers

Collection to string using linq

I have a class public class Person { public string FirstName { get; set; } public string LastName { get; set; } } List PersonList = new List(); PersonList.Add(new Person() { FirstName = "aa", LastName = "AA" }…
TheBoubou
  • 19,487
  • 54
  • 148
  • 236
23
votes
5 answers

Best practices for dealing with LINQ statements that result in empty sequences and the like?

...I'm a little confused, or unsure about how to deal with errors that arise from LINQ statements. I just love being able to pull one or more items from a collection, based on some criteria... with a single line of code. That's pretty awesome. But…
Dave
  • 14,618
  • 13
  • 91
  • 145
23
votes
6 answers

How to split an array into a group of n elements each?

What is the best way to group an array into a list of array of n elements each in c# 4. E.g string[] testArray = { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8" }; should be split into if we take n=3. string[] A1 = {"s1", "s2", "s3"}; string[] A2…
Amitabh
  • 59,111
  • 42
  • 110
  • 159
23
votes
3 answers

Dynamic + linq compilation error

I'll say up front that I am doing some really scary things with linq on dynamic data. But I can't figure out why this query fails to compile: Error 1 The property '<>h__TransparentIdentifier0' cannot be used with type arguments public class…
Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
23
votes
3 answers

Trim() a list of strings using dynamic query language to produce a trimmed IQueryable

Is this possible, or am I just trying to way overly shorten my code? I thought it might be something like: IQueryable trimmedStrs = untrimmedStrsArr.AsQueryable().All(s => s.Trim()); But that's no good :(
Matt W
  • 11,753
  • 25
  • 118
  • 215
23
votes
8 answers

Is there a better way of calling LINQ Any + NOT All?

I need to check if a sequence has any items satisfying some condition but at the same time NOT all items satisfying the same condition. For example, for a sequence of 10 items I want to have TRUE if the sequence has at least one that satisfy the…
Stécy
  • 11,951
  • 16
  • 64
  • 89
23
votes
1 answer

Entity Framework 6 Code First Custom Functions

I'm trying something similar to this: How to use scalar-valued function with linq to entity? However I'm not using EDMX, but instead just DbContext and code first. I've come across this: https://codefirstfunctions.codeplex.com/ But the usage isn't…
AndrewC
  • 321
  • 1
  • 2
  • 11
23
votes
1 answer

Linq to Select Parent Objects Where Child Objects Have a Matching Child Object

How would I go about writing a LINQ statement that selects the parent objects that have a matching child object in it's collection? Here's example classes. class Parent { int ID { get; set; } string Name { get; set; } List
Will Strohl
  • 1,646
  • 2
  • 15
  • 32