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
744
votes
24 answers

Dynamic LINQ OrderBy on IEnumerable / IQueryable

I found an example in the VS2008 Examples for Dynamic LINQ that allows you to use a SQL-like string (e.g. OrderBy("Name, Age DESC")) for ordering. Unfortunately, the method included only works on IQueryable. Is there any way to get this…
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
384
votes
11 answers

Remove duplicates in the list using linq

I have a class Items with properties (Id, Name, Code, Price). The List of Items is populated with duplicated items. For ex.: 1 Item1 IT00001 $100 2 Item2 IT00002 $200 3 Item3 IT00003 …
Prasad
  • 58,881
  • 64
  • 151
  • 199
298
votes
12 answers

Sorting a list using Lambda/Linq to objects

I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of objects. Ex: public class Employee { public string FirstName {set; get;} public string LastName {set; get;} public DateTime DOB {set;…
DotnetDude
  • 11,617
  • 35
  • 100
  • 158
226
votes
5 answers

Linq select objects in list where exists IN (A,B,C)

I have a list of orders. I want to select orders based on a set of order statuses. So essentially select orders where order.StatusCode in ("A", "B", "C") // Filter the orders based on the order status var filteredOrders = from order in orders.Order …
MartinS
  • 6,134
  • 10
  • 34
  • 40
214
votes
4 answers

Code equivalent to the 'let' keyword in chained LINQ extension method calls

Using the C# compilers query comprehension features, you can write code like: var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" }; var result = from animalName in names let nameLength = animalName.Length where…
LBushkin
  • 129,300
  • 32
  • 216
  • 265
203
votes
7 answers

Find() vs. Where().FirstOrDefault()

I often see people using Where.FirstOrDefault() to do a search and grab the first element. Why not just use Find()? Is there an advantage to the other? I couldn't tell a difference. namespace LinqFindVsWhere { class Program { static…
KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69
197
votes
10 answers

How can I get LINQ to return the object which has the max value for a given property?

If I have a class that looks like: public class Item { public int ClientID { get; set; } public int ID { get; set; } } And a collection of those items... List items = getItems(); How can I use LINQ to return the single "Item" object…
FrankTheTank
  • 2,133
  • 2
  • 14
  • 8
190
votes
9 answers

Learning about LINQ

Overview One of the things I've asked a lot about on this site is LINQ. The questions I've asked have been wide and varied and often don't have much context behind them. So in an attempt to consolidate the knowledge I've acquired on Linq I'm posting…
lomaxx
  • 113,627
  • 57
  • 144
  • 179
112
votes
1 answer

How can I filter a dictionary using LINQ and return it to a dictionary from the same type

I have the following dictionary: Dictionary dic = new Dictionary(); dic[1] = "A"; dic[2] = "B"; I want to filter the dictionary's items and reassign the result to the same variable: dic = dic.Where (p => p.Key == 1); How…
Homam
  • 23,263
  • 32
  • 111
  • 187
94
votes
5 answers

Check if all items are the same in a List

I have a List(Of DateTime) items. How can I check if all the items are the same with a LINQ query? At any given time there could be 1, 2, 20, 50 or 100 items in the list.
Saif Khan
  • 18,402
  • 29
  • 102
  • 147
85
votes
4 answers

Linq - SelectMany Confusion

From what I understand from the documentation of SelectMany, one could use it to produce a (flattened) sequence of a 1-many relationship. I have following classes public class Customer { public int Id { get; set; } public string Name {…
Jackie Kirby
  • 1,137
  • 1
  • 9
  • 14
77
votes
2 answers

IList to IQueryable

I have an List and I'd like to wrap it into an IQueryable. Is this possible?
Squirrel
  • 1,355
  • 2
  • 16
  • 25
71
votes
43 answers

What's your favorite LINQ to Objects operator which is not built-in?

With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe…
Nappy
  • 3,016
  • 27
  • 39
64
votes
4 answers

How does LINQPad reference other classes, e.g. Books in the LINQ in Action samples

I'm using LINQPad to create LINQ queries in an application I'm bulding. I noticed that in the downloaded LINQ in Action samples, e.g. example 4.04, intellisense shows a class "Books" but I don't see any references or "using" statements in the…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
56
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
  • 12,638
  • 13
  • 62
  • 115
1
2 3
90 91