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

Assigning a Func to an Expression and vice versa

I was tampering with Expressions and I got confused at some points We can assign same LamdaExpression to both Expression and/or Func. But we cannot assign a Func to an Expression (or an Expression to Func). Why cannot we do that? I looked for if a…
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
22
votes
3 answers

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported

Am coding on visual studio 2012 and using Entity Model as my Data layer. However, my drop down control with the Linq statement tend to throw an unhandled exception when the page tries to load (stated title above). Here is my code below; using…
Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
22
votes
5 answers

How do I avoid a memory leak with LINQ-To-SQL?

I have been having some issues with LINQ-To-SQL around memory usage. I'm using it in a Windows Service to do some processing, and I'm looping through a large amount of data that I'm pulling back from the context. Yes - I know I could do this with…
Sam Schutte
  • 6,666
  • 6
  • 44
  • 54
22
votes
4 answers

Convert 'ArrayList' to 'List' (or 'List') using LINQ

I want to convert an ArrayList to a List using LINQ. I tried ToList() but that approach is not working: ArrayList resultsObjects = new ArrayList(); List results = resultsObjects.ToList();
Elvin
  • 2,131
  • 8
  • 23
  • 25
22
votes
1 answer

Calling methods from different Projects in one Solution

There are 3 Projects in 1 Solution. Main manipulations I make from the main file in the 1st Project. However I need to call methods and use classes from the 3rd Project. For example: – 3rd Project has: public DataClasses1DataContext() : …
user1316502
  • 809
  • 2
  • 10
  • 21
22
votes
2 answers

update 2 fields using linq foreach

Can I update 2 fields using linq foreach loop at once? Sample snippet : I have a userdata with Name, Email, CreateTime, LastUpdateTime fields. I have to reset CreateTime and LastUpdateTime for all users. To update i am using 2 calls as…
Vinod Vutpala
  • 803
  • 3
  • 11
  • 17
22
votes
5 answers

conditional include in linq to entities?

I felt like the following should be possible I'm just not sure what approach to take. What I'd like to do is use the include method to shape my results, ie define how far along the object graph to traverse. but... I'd like that traversal to be…
tim
  • 525
  • 2
  • 5
  • 9
22
votes
3 answers

Difference between “Equals” and “SequenceEqual”?

Is there any cases in which: Equals(MyList1, MyList2) != MyList1.SequenceEqual(MyList2); And what is the difference between: Equals(obj1, obj2) and obj1.Equals(obj2) Thanks.
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
21
votes
6 answers

Debugging LINQ Queries

We've been doing a lot of work with LINQ lately, mainly in a LINQ-to-Objects sense. Unfortunately, some of our queries can be a little complicated, especially when they start to involve multiple sequences in combinations. It can be hard to tell…
GWLlosa
  • 23,995
  • 17
  • 79
  • 116
21
votes
2 answers

Doing multiple joins within a LINQ statement

Can someone help me translate the following SQL query into a LINQ format. SELECT a.ID, a.HostID, h.URL, a.SourceURL, a.TargetURL, c.Value, a.ExtFlag FROM Link a INNER JOIN Host h ON h.ID = a.HostID INNER…
stats101
  • 1,837
  • 9
  • 32
  • 50
21
votes
6 answers

What is the best way to check two List lists for equality in C#

There are many ways to do this but I feel like I've missed a function or something. Obviously List == List will use Object.Equals() and return false. If every element of the list is equal and present in the same location in the opposite list then I…
Spence
  • 28,526
  • 15
  • 68
  • 103
21
votes
3 answers

How to get specific element Count in XML or XElement variable

Consider this XML: 1000 Nima Agha 1001 Ligha Ligha
Arian
  • 12,793
  • 66
  • 176
  • 300
21
votes
19 answers

Debugging LINQ to SQL SubmitChanges()

I am having a really hard time attempting to debug LINQ to SQL and submitting changes. I have been using http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx, which works great for debugging simple queries. I'm working…
ben
  • 3,126
  • 3
  • 37
  • 51
21
votes
4 answers

How can I specify an index hint in Entity Framework?

sql select * from table1 with(index=IX_table1_1) Linq to sql using ado.net entity would like to write the above code. I could not find entity in particular, the use of the index hint. linq var querysample = from a in db.table1 select a;
Talat Eri
  • 271
  • 1
  • 3
  • 9
21
votes
3 answers

Best way to convert Dictionary into single aggregate String representation?

How would I convert a dictionary of key value pairs into a single string? Can you do this using LINQ aggregates? I've seen examples on doing this using a list of strings, but not a dictionary. Input: Dictionary map = new…
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173