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

Is AsQueryable method departed in new Mongodb C# driver 2.0rc?

First of all I'm new to MongoDb. In MongoDb C# driver 1.9.x, i can take collections as queryable with AsQueryable method like this. var db = client.GetServer().GetDatabase("test"); var col = db.GetCollection("Video"); var…
ilker unal
  • 502
  • 1
  • 6
  • 15
23
votes
3 answers

Finding symmetric difference with LINQ

I have two collections a and b. I would like to compute the set of items in either a or b, but not in both (a logical exclusive or). With LINQ, I can come up with this: IEnumerable Delta(IEnumerable a, IEnumerable b) { return…
Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
23
votes
1 answer

Unit test error : This function can only be invoked from LINQ to Entities

I am coding a MVC 5 internet application, and I have an expression as follows: public Expression> IsExpiresDateTimeLessThanMinimumDaysLeftInFreeTrialSubscription(int minimumDaysLeftInSubscriptionForEmail) { return Account =>…
Simon
  • 7,991
  • 21
  • 83
  • 163
23
votes
3 answers

LINQ's Func is only called once?

I'm lost on what keywords to google for... Could anyone please point me to an MSDN page or SO answer explaining why Foo() is only called once? Especially since First only has a single overload with a predicate. What optimisation is going on…
Ilya Kozhevnikov
  • 10,242
  • 4
  • 40
  • 70
23
votes
3 answers

Why am I getting "Collection was modified; enumeration operation may not execute" when not modifying the enumerated collection?

I have two collections of strings: CollectionA is a StringCollection property of an object stored in the system, while CollectionB is a List generated at runtime. CollectionA needs to be updated to match CollectionB if there are any differences. So…
Grace Note
  • 3,205
  • 4
  • 35
  • 55
23
votes
2 answers

Why can't I call an extension method from a base class of the extended type‏?

I'm trying add the ability to lookup elements in a List> by overriding the indexer. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace…
synepis
  • 1,292
  • 16
  • 28
23
votes
3 answers

Excluding one item from list (by Index), and take all others

There is a List containing some set of numbers. Randomly I select an index, which will be processed separately (call it master). Now, I want to exclude this particular index, and get all other elements of List (call them slave). var items = new…
Ajay
  • 18,086
  • 12
  • 59
  • 105
23
votes
3 answers

How to join two Lists based on common property

Suppose I have two Lists where myObject consists of the two properties Id (of type Int) and Value (of type Double) I need to get a list out of these two lists that is made of (anonymous) objects like this: Id, [Double value from List 1],…
Rob
  • 11,492
  • 14
  • 59
  • 94
23
votes
2 answers

Entity Framework recursively include collection for each entity from included collection

I have the following where I am trying to include the addresses of the people in the cities of the countries. Country country = _db.Countries .Include(p=>p.Cities.People.????) .Where(....) Not sure how to work it?
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
23
votes
7 answers

Which ORM to use with SQL Azure?

Just wondering what everyones thoughts on what ORM to use for SQL Azure? I'm fairly comfortable using LINQ-to-SQL and I believe it is possible to get it working with SQL Azure. However, from my understanding (correct me if I'm wrong), no further…
Jamie Chapman
  • 4,229
  • 5
  • 29
  • 47
23
votes
2 answers

How to ignore case sensitivity in StartsWith for LINQ FindAll?

I have the following code: ContactList = ContactList.FindAll(p => p.DeptName.StartsWith(optAlpha.SelectedItem.Value)).ToList(); If DeptName="test" and optAlpha.SelectedItem.Value="T", it doesn't work. I tried with the following code, still doesn't…
WinFXGuy
  • 1,527
  • 7
  • 26
  • 47
23
votes
5 answers

In LINQ-SQL, wrap the DataContext is an using statement - pros cons

Can someone pitch in their opinion about pros/cons between wrapping the DataContext in an using statement or not in LINQ-SQL in terms of factors as performance, memory usage, ease of coding, right thing to do etc. Update: In one particular…
hIpPy
  • 4,649
  • 6
  • 51
  • 65
23
votes
3 answers

How to get a list of the grouped values in linq groupby?

Linq newbie here, struggling with my first GroupBy query. I have a list of objects of type KeywordInstance which represents a keyword, and the ID of the database record to which the keyword was applied. Keyword RecordID macrophages …
Rumi P.
  • 1,688
  • 3
  • 23
  • 31
23
votes
2 answers

.OrderBy(DayOfWeek) to treat Sunday as the end of the week

I'm ordering a number of objects by their System.DayOfWeek property. DayOfWeek treats Sunday as the start of the week, whereas I would like it to be ordered so it appears at the end. It's just an enum, so I can't modify it. However I've read that I…
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
23
votes
5 answers

Enumerating Collections that are not inherently IEnumerable?

When you want to recursively enumerate a hierarchical object, selecting some elements based on some criteria, there are numerous examples of techniques like "flattening" and then filtering using Linq : like those found here : link text But, when you…
BillW
  • 3,415
  • 4
  • 27
  • 46