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

What is the big deal with IQueryable?

I've seen a lot of people talking about IQueryable and I haven't quite picked up on what all the buzz is about. I always work with generic List's and find they are very rich in the way you can "query" them and work with them, even run LINQ queries…
jjr2527
  • 440
  • 1
  • 6
  • 16
21
votes
4 answers

SelectMany cannot be inferred from the usage

I get the following error when I try to compile my code: The type arguments for method 'System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable, System.Func>)' cannot be inferred from the usage. Try specifying the type…
Serge Profafilecebook
  • 1,165
  • 1
  • 14
  • 32
21
votes
4 answers

Selecting multiple columns with linq query and lambda expression

I'm new to C# ASP.NET, and am working on my first application. I'm trying to create a linq statment that return an arrary. I have a table of products. I want to be able to select name, id, and price, for each product where the status == 1. I am…
Mark
  • 4,773
  • 8
  • 53
  • 91
21
votes
6 answers

How to do Python's zip in C#?

Python's zip function does the following: a = [1, 2, 3] b = [6, 7, 8] zipped = zip(a, b) result [[1, 6], [2, 7], [3, 8]]
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
21
votes
2 answers

Why does XmlReader skip every other element if there is no whitespace separator?

I'm seeing strange behavior when I try to parse XML using the LINQ XmlReader class. Test case below: it looks like whether I use (XElement)XNode.ReadFrom(xmlReader) or one of the Read() methods on XmlReader, it misses the second bar elements in the…
Joe Smith
  • 213
  • 1
  • 2
  • 4
21
votes
5 answers

IndexOf with Linq, accepting lambda expression

Is there a way to find the index from list of partial prefixes with Linq, something like: List PartialValues = getContentsOfPartialList(); string wholeValue = "-moz-linear-gradient(top, #1e5799 0%, #7db9e8 100%)"; int indexOfPartial =…
Annie
  • 3,090
  • 9
  • 36
  • 74
21
votes
1 answer

LINQ can't use string.contains?

This is my code: string queryString = "Marco".ToLower(); utenti = db.User.Where(p => queryString.Contains(p.Nickname.ToLower()) || queryString.Contains(p.Nome.ToLower()) || …
markzzz
  • 47,390
  • 120
  • 299
  • 507
21
votes
4 answers

Why IEnumerable slow and List is fast?

Came across this code. var dic = new Dictionary(); for(int i=0; i<20000; i++) { dic.Add(i, i.ToString()); } var list = dic.Where(f => f.Value.StartsWith("1")).Select(f => f.Key);//.ToList(); //uncomment for fast results…
ren
  • 3,843
  • 9
  • 50
  • 95
21
votes
3 answers

How I can filter a dataTable with Linq to datatable?

hi how i can filter a datatable with linq to datatable? I have a DropDownList and there I can select the value of the Modul Column. Now I want to filter the DataTable with this Modul Column. here is my datatable structure: User | Host | TimeDiff…
Tarasov
  • 3,625
  • 19
  • 68
  • 128
21
votes
3 answers

Why does adding an unnecessary ToList() drastically speed this LINQ query up?

Why does forcing materialization using ToList() make my query orders of magnitude faster when, if anything, it should do the exact opposite? 1) Calling First() immediately // "Context" is an Entity Framework DB-first model var query = from…
JoeCool
  • 4,392
  • 11
  • 50
  • 66
21
votes
11 answers

LINQ: Get Table Column Names

Using LINQ, how can I get the column names of a table? C# 3.0, 3.5 framework
Lennie De Villiers
  • 5,119
  • 7
  • 31
  • 30
21
votes
5 answers

LINQ sort a flat list based on childorder

I am currently trying to figure out a good way to sort my elements with LINQ and C#, but I am kinda failing to do so. For the problem let assume you have the following Table ---TempTable ID (int) ParentID (int) Name (varchar) SortOrder (int) The ID…
Rand Random
  • 7,300
  • 10
  • 40
  • 88
21
votes
4 answers

LINQ Get Distinct values and fill LIST

I am trying to figure out if I can use LINQ to provide me with the distinct values of some data I have in a DataTable (FirstName, LastName, QTY). I can get the distinct values and fill my List, but I have to run two different LINQ queries to get…
scarpacci
  • 8,957
  • 16
  • 79
  • 144
21
votes
5 answers

Cannot implicitly convert type '.List' to '.List'

In the following code that returns a list: public List GeAllCust() { var results = db.Customers .Select(x => new { x.CustName, x.CustEmail, x.CustAddress, x.CustContactNo }) .ToList() return results; } I get an…
Abdul Khaliq
  • 2,139
  • 4
  • 27
  • 31
21
votes
2 answers

Regex in Linq statement?

I'm writing a short C# to parse a given XML file. But 1 of the tag values can change, but always includes words "Fast Start up" (disregarding case and spaces, but needs to be in the same order) in the where clause. I'm not sure how I would do this…
jerryh91
  • 1,777
  • 10
  • 46
  • 77
1 2 3
99
100