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
50
votes
8 answers

Linq OrderBy against specific values

Is there a way in Linq to do an OrderBy against a set of values (strings in this case) without knowing the order of the values? Consider this data: A B A C B C D E And these variables: string firstPref, secondPref, thirdPref; When the values are…
Jonathan Parker
  • 6,497
  • 3
  • 38
  • 54
48
votes
2 answers

Using LINQ, select list of objects inside another list of objects

public class ClassA { public string MyString {get; set;} } public class ClassB { public List MyObjects {get; set;} } List classBList = new List(); var results = (from i in classBList select…
Tom
  • 1,180
  • 1
  • 10
  • 24
46
votes
3 answers

Sequence contains no elements exception in linq without even using Single

I am not using Single in LINQ below, but I am still getting a 'Sequence contains no elements' exception: allNames = StockCollection.Where((s) => s.Name.IndexOf("A") == 0) .Select((s) => s.Name) …
D J
  • 6,418
  • 10
  • 38
  • 73
46
votes
5 answers

C# Linq where clause as a variable

I am trying to make a LINQ statement where the where clause comes from a variable. For example: string whereClause = address.zip == 23456; var x = from something in someList where whereClause; Is this possible? I cannot seem to get it to work.…
kds6253
  • 795
  • 1
  • 11
  • 17
45
votes
7 answers

LINQ identity function?

Just a little niggle about LINQ syntax. I'm flattening an IEnumerable> with SelectMany(x => x). My problem is with the lambda expression x => x. It looks a bit ugly. Is there some static 'identity function' object that I can use…
Joe
  • 42,600
  • 24
  • 134
  • 225
44
votes
2 answers

Getting keys from a Lookup

How do I get the collection of keys from a Lookup<> I created through the .ToLookup() method? I have a lookup which maps int-values to groups of instances of a custom class. I need a collection of all the int keys that the lookup contains. Any way…
magnattic
  • 11,529
  • 10
  • 59
  • 109
44
votes
5 answers

With Entity Framework is it better to use .First() or .Take(1) for "TOP 1"?

We are implementing some EF data repositories, and we have some queries which would include TOP 1 I have read many posts suggesting to use .Take(1) The code I'm reviewing uses .First() I understand that both of these produce the same result for…
Matthew
  • 9,896
  • 5
  • 43
  • 95
41
votes
9 answers

How to debug a LINQ Statement

I have a Linq to objects statement var confirm = from l in lines.Lines where (l.LineNumber == startline.LineNumber) || (l.LineNumber == endline.LineNumber) select l; The confirm object is returning an 'Object Null or Not A Reference' at at…
johnc
  • 36,657
  • 37
  • 96
  • 137
41
votes
5 answers

"Nested foreach" vs "lambda/linq query" performance(LINQ-to-Objects)

In performance point of view what should you use "Nested foreach's" or "lambda/linq queries"?
JSC
  • 3,685
  • 3
  • 24
  • 24
39
votes
6 answers

LINQ to SQL and a running total on ordered results

I want to display a customer's accounting history in a DataGridView and I want to have a column that displays the running total for their balance. The old way I did this was by getting the data, looping through the data, and adding rows to the…
Ecyrb
  • 1,950
  • 2
  • 25
  • 43
37
votes
3 answers

Recursive control search with LINQ

If I wanted to find checked check boxes on an ASP.NET page I could use the following LINQ query. var checkBoxes = this.Controls .OfType() .TakeWhile(cb => cb.Checked); That works fine if…
PhilGriffin
  • 638
  • 1
  • 5
  • 9
34
votes
5 answers

LINQ return items in a List that matches any Names (string) in another list

I have 2 lists. 1 is a collection of products. And the other is a collection of products in a shop. I need to be able to return all shopProducts if the names match any Names in the products. I have this but it doesn't seem to work. Any ideas? …
Martin
  • 22,234
  • 53
  • 190
  • 309
33
votes
5 answers

Select distinct by two properties in a list

I have a list that contains properties of type Guid and DateTime (as well as other properties). I would like to get rid of all of the items in that list where the Guid and DateTime are the same (except one). There will be times when those…
user1304444
  • 1,493
  • 2
  • 20
  • 38
32
votes
3 answers

LINQ SelectMany and Where extension method ignoring nulls

I have the below example code, and I am interested to know how I can make this any cleaner, possibly through better use of SelectMany(). At this point the QuestionList property will not be null. All I want is a list of answerRows that are not null,…
Pricey
  • 5,461
  • 11
  • 57
  • 80
31
votes
3 answers

How to use async lambda with SelectMany?

I'm getting the following error when trying to use an async lambda within IEnumerable.SelectMany: var result = myEnumerable.SelectMany(async (c) => await Functions.GetDataAsync(c.Id)); The type arguments for method 'IEnumerable …
CodingIntrigue
  • 65,670
  • 26
  • 159
  • 166
1
2
3
89 90