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
696
votes
20 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
  • 74,152
  • 28
  • 154
  • 191
341
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
  • 56,343
  • 61
  • 142
  • 199
280
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,119
  • 30
  • 94
  • 156
195
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
  • 121,016
  • 31
  • 208
  • 258
189
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
  • 104,787
  • 56
  • 140
  • 177
181
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
  • 5,563
  • 10
  • 32
  • 39
173
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
  • 1,893
  • 2
  • 13
  • 8
172
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
  • 8,717
  • 7
  • 44
  • 66
94
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
  • 21,556
  • 28
  • 104
  • 181
82
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,097
  • 1
  • 9
  • 13
76
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. Thanks
Saif Khan
  • 17,334
  • 28
  • 97
  • 144
73
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,275
  • 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
  • 2,833
  • 23
  • 38
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
  • 176,854
  • 291
  • 683
  • 1,015
51
votes
2 answers

How to merge a collection of collections in Linq

I would like to be able to fusion an IEnumerable> into IEnumerable (i.e. merge all individual collections into one). The Union operators only applies to two collections. Any idea?
Joannes Vermorel
  • 8,551
  • 10
  • 60
  • 95
1
2 3
89 90