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
30
votes
5 answers

linq to entities vs linq to objects - are they the same?

I usually use the term entity to represent a business data object and in my mind, the linq to entities and linq to objects were the same. Is that not correct?
Alex J
  • 1,497
  • 2
  • 25
  • 41
30
votes
2 answers

Linq Except with custom IEqualityComparer

I am trying to find the difference between two generic lists, as in the example below. Even though t1 and t2 contain the same properties, they are not the same object, so I have need to implement an IEqualityComparer. This appears to be working…
sgmoore
  • 14,470
  • 5
  • 37
  • 65
30
votes
4 answers

Conversion from Int array to string array

When I am converting array of integers to array of string, I am doing it in a lengthier way using a for loop, like mentioned in sample code below. Is there a shorthand for this? The existing question and answers in SO are about int[] to string (not…
InfantPro'Aravind'
  • 11,372
  • 20
  • 76
  • 112
29
votes
4 answers

LINQ "MaxOrDefault"?

I'm new to LINQ. I need to compute new_id as follows: public class C_Movement { public int id=-1; public static ObservableCollection list=new ObservableCollection(); // ... } int new_id = (C_Movement.list.Count==0) ? 0…
Telaclavo
  • 2,209
  • 2
  • 15
  • 15
29
votes
5 answers

LINQ - is SkipWhile broken?

I'm a bit surprised to find the results of the following code, where I simply want to remove all 3s from a sequence of ints: var sequence = new [] { 1, 1, 2, 3 }; var result = sequence.SkipWhile(i => i == 3); // Oh noes! Returns { 1, 1, 2, 3 } Why…
Judah Gabriel Himango
  • 55,559
  • 37
  • 152
  • 206
28
votes
3 answers

Create a list of one object type from a list of another using Linq

If I have classes of Type A and B: public class A { public int TotalCount; public string Title; } public class B { public int Count; public string Title; } I have a list of instances A instances, what is the most efficient way to…
Jeremy
  • 40,978
  • 63
  • 191
  • 304
26
votes
6 answers

how to find the longest string in a string[] using LINQ

I have an array of strings of variable length. Currently I have a loop that iterates through the array to find the longest string in array. Is there any way I could use LINQ to write it in more efficient and / or cleaner way?
vrrathod
  • 1,130
  • 2
  • 16
  • 27
26
votes
4 answers

how to `.Take()` on a string and get a string at the end?

LINQ to Objects supports queries on string objects but when I use code such as below: string SomeText = "this is some text in a string"; return SomeText.Take(6).ToString(); All I get…
rtpHarry
  • 12,334
  • 4
  • 39
  • 59
26
votes
5 answers

Using LINQ to group a list of objects

I have an object: public class Customer { public int ID { get; set; } public string Name { get; set; } public int GroupID { get; set; } } I return a list that may look like the following: List CustomerList = new…
user339160
25
votes
9 answers

Filtering lists using LINQ

I've got a list of People that are returned from an external app and I'm creating an exclusion list in my local app to give me the option of manually removing people from the list. I have a composite key which I have created that is common to both…
keeno
24
votes
8 answers

Using LINQ to Objects to find items in one collection that do not match another

I want to find all items in one collection that do not match another collection. The collections are not of the same type, though; I want to write a lambda expression to specify equality. A LINQPad example of what I'm trying to do: void Main() { …
TrueWill
  • 23,842
  • 7
  • 88
  • 133
22
votes
4 answers

LINQ to Objects Join two collections to set values in the first collection

I have the following Entity Framework query: var results = from r in db.Results select r; I'm using AutoMapper to map to another type: var mapped = Mapper.Map, IEnumerable>(results); In my…
Dismissile
  • 30,816
  • 34
  • 167
  • 253
22
votes
5 answers

C# Merging 2 dictionaries

I'm developing an app in C# targeting .NET 3.5. In it, I have 2 similar dictionaries that contain validation criteria for a specific set of elements in my app. Both dictionaries have identical signatures. The first dictionary has the default…
psubsee2003
  • 8,115
  • 8
  • 58
  • 76
22
votes
2 answers

Does .GroupBy() guarantee order in its groupings?

Say I have an (ordered) sequence of animals: Eagle Elephant Tarantula Terrapin Tiger and I group by first letter: Animals.GroupBy(animal => animal.First()) will the elements of the IGroupings in the resulting sequence be in the same order as the…
spender
  • 106,080
  • 28
  • 202
  • 324
21
votes
4 answers

how to query LIST using linq

Suppose if I add person class instance to list and then I need to query the list using linq. List lst=new List(); lst.add(new person{ID=1,Name="jhon",salary=2500}); lst.add(new person{ID=2,Name="Sena",salary=1500}); lst.add(new…
Mou
  • 13,749
  • 38
  • 131
  • 248
1 2
3
89 90