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.

1347 questions
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
  • 645
  • 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,035
  • 13
  • 41
  • 65
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
  • 28,102
  • 6
  • 74
  • 88
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
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
  • 39,502
  • 20
  • 127
  • 156
19
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
  • 108,589
  • 35
  • 194
  • 274
18
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
  • 503
  • 2
  • 7
  • 20
18
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
  • 59,154
  • 14
  • 90
  • 146
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
  • 8,867
  • 7
  • 51
  • 94
17
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
  • 480
  • 1
  • 3
  • 15
17
votes
3 answers

Does LINQ to Objects keep its order

I have a List and instead want to convert them for simple processing to a List, doing the following: List persons = GetPersonsBySeatOrder(); List seatNames = persons.Select(x =>…
arserbin3
  • 5,662
  • 8
  • 31
  • 52
17
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
  • 36,331
  • 28
  • 120
  • 198
16
votes
5 answers

How can I use LINQ to avoid nested loops?

I've been reading about LINQ to Objects, and now my colleagues want me to present it to them. Now, I have an OK understanding of the operators and the syntax choices, but I've heard you can avoid heavy nested loops by using LINQ. I'm having trouble…
Skywise
  • 309
  • 2
  • 12
16
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,662
  • 10
  • 40
  • 48
16
votes
4 answers

Why does LINQ query throw an exception when I attempt to get a count of a type

public readonly IEnumerable PeriodToSelect = new string[] { "MONTH" }; var dataCollection = from p in somedata from h in p.somemoredate where h.Year > (DateTime.Now.Year - 2) where PeriodToSelect.Contains(h.TimePeriod) …
thenth
  • 481
  • 2
  • 5
  • 8