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

Cannot implicitly convert type IEnumerable to IQueryable

Obfuscated Scenario: A person has zero, one or many pets. Using Linq to Sql, the need is to get an IQueryable list of pets for the given personID. Here's the poorly mangled/butchered/obfuscated portion of the ERD: Code: public IQueryable
p.campbell
  • 98,673
  • 67
  • 256
  • 322
23
votes
2 answers

LINQ OrderBy not ordering .. changing nothing .. why?

Any idea why the LINQ OrderBy is not working in following code, (have no errors but method does not sort ...) First my own type public class IQLinksView { public int id { get; set; } public int catid { get; set; } public…
user1415838
  • 233
  • 1
  • 2
  • 5
23
votes
1 answer

Get different and common items in two arrays with LINQ

For example, I have two arrays: var list1 = string[] {"1", "2", "3", "4", "5", "6"}; var list2 = string[] {"2", "3", "4"}; What I'm trying to do is - Get common items from list1 and list2 (eg. {"2", "3", "4"}) Get different items list1 and list2…
Ye Myat Aung
  • 1,783
  • 11
  • 31
  • 49
23
votes
2 answers

How can I combine two lambda expressions without using Invoke method?

I have two lambda expressions: Expression> e1 = i = >i.FName.Contain("john"); and Expression> e2 = i => i.LName.Contain("smith"); the i type, comes from my poco entities, that can't used with invoke. I…
PickleRick
  • 419
  • 1
  • 5
  • 13
23
votes
4 answers

Why is Array.Sort() so slow compared to LINQ?

I made quick testing application to compare LINQ sorting to Array.Sort on my custom objects. Array.Sort seems extremely slow! I made my custom class like this: class Person : IComparable { public int Age { get; set; } public string…
Tomasz Sikora
  • 1,649
  • 3
  • 19
  • 30
22
votes
5 answers

How to convert list of arrays into a multidimensional array

I need to convert the following collection into double[,]: var ret = new List(); All the arrays in the list have the same length. The simplest approach, ret.ToArray(), produces double[][], which is not what I want. Of course, I can…
Arne Lund
  • 2,366
  • 3
  • 26
  • 39
22
votes
6 answers

How can I use linq to sort by multiple fields?

I'm creating a mock data source that I want to be able to pass in a list of SortExpressions on. public SortExpression(string name, SortDirection direction) { this.name = name; this.direction = direction; } Update with Jon Skeet's code and…
rball
  • 6,925
  • 7
  • 49
  • 77
22
votes
2 answers

Confused about passing Expression vs. Func arguments

I'm having some trouble understanding the differences between how Expressions and Funcs work. This problem turned up when someone changed a method signature from: public static List ThingList(Func aWhere) To public static…
Erix
  • 7,059
  • 2
  • 35
  • 61
22
votes
2 answers

Linq to return string

I am not sure why the following does not return a value for Vend as a string . When I check for the value of vend it says: System.Data.Objects.ObjectQuery``1[System.String] string vend = (from vnd in db.Vendors where vnd.VendorID…
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
22
votes
1 answer

Disjoint Union in LINQ

I have two sets (ILists) where I need all the items from the 1st list, where the item is not in the second list. Can anyone point me to the best way of achieving this with a LINQ statement?
Chris S
  • 64,770
  • 52
  • 221
  • 239
22
votes
2 answers

Paginated search results with LINQ to SQL

What's the best pattern to get paginated results with LINQ to SQL? I have the following scenario: Suppose I want to search items table by description. I can easily do: public IQueryable FindItemsByDescription(string description) { return…
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
22
votes
2 answers

NHibernate and Repository pattern

What is the recommended approach to take with Nhibernate + Repository pattern? There are so many different articles and opinions around that I am not sure what path to take. Take this lengthy article, for example. It gives an example of Query…
doe
  • 223
  • 2
  • 5
22
votes
5 answers

Using LINQ in F#?

Given that the basic syntax and semantics of a language like F# are already designed to provide exactly what LINQ provides to C#/VB as an add one, why should I be using LINQ when programming in F#? What do I gain, and what might I lose?
Armentage
  • 12,065
  • 8
  • 33
  • 33
22
votes
7 answers

How can I use Nhibernate to retrieve data when the "WHERE IN()" have thousands of values? (too many parameters in the sql)

The problem: Nhibernate parses each value in the "WHERE IN()" sql as parameters and MS SQL server doesn't support enough parameters (over 2000). I am using Nhibernate with Linq to retrive my data from the SQL server and I need to load alot of…
Kenneth_hj
  • 485
  • 1
  • 4
  • 11
22
votes
3 answers

removing duplicates in a list with linq

Suppose you have a list of MyObject like this: public class MyObject { public int ObjectID {get;set;} public string Prop1 {get;set;} } How do you remove duplicates from a list where there could be multiple instance of objects with the same…
frenchie
  • 51,731
  • 109
  • 304
  • 510