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

What does the "New ... With" syntax do in VB Linq?

What (if any) is the difference between the results of the following two versions of this VB Linq query? ' assume we have an XElement containing employee details defined somewhere else Dim ee = From e In someXML. _ Select New With…
Steve Davies
  • 562
  • 3
  • 7
  • 14
22
votes
2 answers

Why can't I use the enumerator of an array, instead of implementing it myself?

I have some code like this: public class EffectValues : IEnumerable { public object [ ] Values { get; set; } public IEnumerator GetEnumerator ( ) { return this.Values.GetEnumerator ( ); } …
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
22
votes
12 answers

Find character with most occurrences in string?

For example, I have a string: "abbbbccd" b has the most occurrences. When using C++, the easiest way to handle this is inserting each character into a map<>. Do I have to do the same thing in C#? Is there an elegant way to do it using LINQ?
roxrook
  • 13,511
  • 40
  • 107
  • 156
22
votes
6 answers

Different ways of using SelectMany()

I'd like to know how to use SelectMany(). It seems to take so many arguments and from my own research I noticed that SelectMany() might be the 'father' of all other select operations.
Ali Tarhini
  • 5,278
  • 6
  • 41
  • 66
22
votes
12 answers

Where to draw the line - is it possible to love LINQ too much?

I recently found LINQ and love it. I find lots of occasions where use of it is so much more expressive than the longhand version but a colleague passed a comment about me abusing this technology which now has me second guessing myself. It is my…
BobTheBuilder
  • 2,455
  • 2
  • 27
  • 39
22
votes
7 answers

Is it Possible to call a Stored Procedure using LINQ in LINQPad?

In visual studio you have the nice designer that encapsulates a stored proc with a nifty little method. I totally love LINQPad and use it on a daily basis at work (haven't had a need to open up SQL Studio for my job since I've been using it!) and…
Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
22
votes
2 answers

Adding A Custom Property To Entity Framework?

I am using the Entity Framework for the first time and want to know if the following is possible - I have generated my classes from the DB, and have one called Category. Obviously it has all my fields in the table (ID, CategoryName, SortOrder etc..)…
YodasMyDad
  • 9,248
  • 24
  • 76
  • 121
22
votes
3 answers

Select only the lowest values with Linq

Hi I got the following linq for ordering my input after lowest value. But i would like it to only output the lowest values. var sortedDict = (from entry in x where entry.Value > 0 orderby entry.Value ascending select entry); Now if it gets the…
gulbaek
  • 2,481
  • 13
  • 44
  • 65
22
votes
5 answers

How do I ensure a sequence has a certain length?

I want to check that an IEnumerable contains exactly one element. This snippet does work: bool hasOneElement = seq.Count() == 1 However it's not very efficient, as Count() will enumerate the entire list. Obviously, knowing a list is empty or…
guhou
  • 1,732
  • 12
  • 32
22
votes
7 answers

Linq to return ALL pairs of elements from two lists?

Given lists l1 = {1, 2} and l2 = {4, 5, 6 } I want to get a new list that has elements: rez = { {1, 4}, {1, 5}, {1, 6}, {2, 4}, {2, 5}, {2, 6} } Suggestions?
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
22
votes
5 answers

C# .First() vs [0]

Interested, does approaches has any differences. So, I created two snippets. Snippet A List a = new List(); a.Add(4); a.Add(6); int b = a.First(); and Snippet B List a = new List(); a.Add(4); a.Add(6); int b = a[0]; In…
Arman Hayots
  • 2,459
  • 6
  • 29
  • 53
22
votes
4 answers

LINQ - Get all items in a List within a List?

I'm currently working my way through the learning curve that is LINQ and I could really use some assistance. I don't know if what I want is possible, but if I had to wager, I bet it is. I currently have a list of objects called _tables and each of…
Sonny Boy
  • 7,848
  • 18
  • 76
  • 104
22
votes
4 answers

How is it possible that "RemoveAll" in LINQ is much faster than iteration?

The following code: List intervals = new List(); List points = new List(); //Initialization of the two lists // [...] foreach (var point in points) { intervals.RemoveAll (x => x.Intersects (point)); } is at least…
Brainless
  • 1,522
  • 1
  • 16
  • 30
22
votes
5 answers

Interfaces, Inheritance, Implicit operators and type conversions, why is it this way?

I'm working with a class library called DDay ICal. It is a C# wrapper for the iCalendar System implemented in Outlook Calendars, and many many many more systems. My question is derived from some work I was doing with this system. There are 3 objects…
22
votes
6 answers

Unique list of items using LINQ

I've been using LINQ for a while now, but seem to be stuck on something with regards to Unique items, I have the folling list: List stock = new List(); This has the following Properties: string ID , string Type, string Description,…
RoguePlanetoid
  • 4,516
  • 7
  • 47
  • 64