Questions tagged [linq-to-objects]

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML.

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML.

For example, say that myCats is a list of Cat objects. Then you can pick your favorite cat by using:

Cat myFavorite = myCats.Single(c => c.Score == myCats.Max(x => x.Score));

To get an alphabetical listing of your cats, use this:

var listing = myCats.OrderBy(c => c.Name);

To get all cats that have caught more than 20 mice, use the following:

var champions = myCats.Where(c => c.MiceCaught.Count > 20);

You can also use a more SQL-like syntax:

var champions = from cat in myCats where cat.MiceCaught.Count > 20 select cat;

LINQ to Objects thus uses the same familiar syntax as the other LINQ implementations such as LINQ to Entities and LINQ to SQL.

1356 questions
22
votes
2 answers

LINQ Lambda vs Query Syntax Performance

I saw a LINQ query syntax in my project today which was counting items with a specific condition from a List like this: int temp = (from A in pTasks where A.StatusID == (int)BusinessRule.TaskStatus.Pending select…
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
21
votes
2 answers

Joining three tables and using a left outer join

I have three tables. Two of them join equally but one will need to join with a left. I'm finding a lot of code to do this in linq but between two tables only. Here is the SQL code that I'm trying to re-code within LINQ. SELECT PRSN.NAME …
MdeVera
  • 647
  • 5
  • 8
  • 22
21
votes
1 answer

Lambda Expression for join

public class CourseDetail { public CourseDetail(); public string CourseId { get; set; } public string CourseDescription { get; set; } public long CourseSer { get; set; } } public class RefUIDByCourse { …
Radhi
  • 6,289
  • 15
  • 47
  • 68
21
votes
5 answers

Exception handling within a LINQ Expression

I have a simple LINQ-expression like: newDocs = (from doc in allDocs where GetDocument(doc.Key) != null select doc).ToList(); The problem is, GetDocument() could throw an exception. How can I ignore all doc-elements where…
Rocco Hundertmark
  • 545
  • 2
  • 7
  • 20
20
votes
9 answers

Your Favorite LINQ-to-Objects Queries

With LINQ, a lot of programming problems can be solved more easily - and in fewer lines of code. What are some the best real-world LINQ-to-Objects queries that you've written? (Best = simplicity & elegance compared to the C# 2.0 / imperative…
Joe Albahari
  • 30,118
  • 7
  • 80
  • 91
20
votes
6 answers

Filter linq list on property value

I have a List and a List. The customObject class has an ID property. How can I get a List containing only the objects where the ID property is in the List using LINQ? Edit: I accepted Konrads answer because it…
Espo
  • 41,399
  • 21
  • 132
  • 159
20
votes
3 answers

TakeWhile, but get the element that stopped it also

I'd like to use the LINQ TakeWhile function on LINQ to Objects. However, I also need to know the first element that "broke" the function, i.e. the first element where the condition was not true. Is there a single function to get all of the objects…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
19
votes
3 answers

Linq Group on Multiple Fields - VB.NET, Anonymous, Key

I am stumped. I need help. I have a DTO object with duplicates patient address data. I need to get only the unique addresses. Dim PatientAddressDto = New List(Of PatientAddress) {Populate PatientAddressDto with lots of duplicate…
wavedrop
  • 520
  • 1
  • 4
  • 15
19
votes
2 answers

At least one object must implement IComparable

var listair = empcon.OrderBy(x => x.CustomerConnection.OrderBy(y => y.Id)).ToList(); When I am using this statement then I am getting exception "At least one object must implement IComparable" How can I solve this problem?
Amit
  • 191
  • 1
  • 1
  • 3
19
votes
2 answers

C# System.Linq.Lookup Class Removing and Adding values

I'm using Lookup class in C# as my prime data container for the user to select values from two Checked List boxes. The Lookup class is far easier to use than using the class Dictionary>, however I cannot find methods for removing and adding values…
Ahmad Hajou
  • 1,289
  • 5
  • 22
  • 39
18
votes
2 answers

Linq nested list expression

please I need your help with a Linq expression: I have nested objects with lists, this is how the main object hierarchy looks like (each dash is an atribute of the sub-class): Folder -name -List Subfolders -name …
lidermin
  • 2,782
  • 11
  • 43
  • 48
18
votes
5 answers

Optimizing Aggregate for String Concatenation

Update - for those of a facetious frame of mind, you can assume that Aggregate still produces the normal result whatever function is passed to it, including in the case being optimized. I wrote this program to build a long string of integers from 0…
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
18
votes
2 answers

Use LINQ and C# to make a new List from an old List

This should be pretty simple, but I am new at LINQ. I have a List of FillList structs. I'd like to use LINQ to create a new List where instead of having the number of buys and sells, I would have one variable containing…
Addie
  • 1,653
  • 5
  • 21
  • 32
18
votes
5 answers

How to get the first element of IEnumerable

Is there a better way getting the first element of IEnumerable type of this: foreach (Image image in imgList) { picture.Width = (short)image.Columns; picture.Height = (short)image.Rows; break; } This is the exact declaration of the…
Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
18
votes
2 answers

Get a list of distinct items and their count

I have an object, that has many properties but the only two to worry about are: myobject.ID which is an int myobject.Names which is a HashSet Then I have a List of those objects that looks something similar to this: List I use Linq to get…
Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47