Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ equivalent of List<T>.Find()?

Tags:

c#

linq

I'm looking at some code that takes an IEnumerable<T> and converts it to a List<T> so it can use List<T>.Find(predicate):

var myEnumerable = ...;
var myList = new List<T>(myEnumerable);
var match = myList.Find(value => value.Aaa == aaa && value.Bbb == bbb);

Is there a way to rewrite this using the LINQ extension methods that has the same effect, but without building an extra List<T> as an intermediate step?

The FirstOrDefault(source, predicate) extension method looks like a good candidate, but trying to figure out if it's exactly equivalent to Find is making my head hurt.

like image 845
Joe White Avatar asked Jun 29 '11 23:06

Joe White


People also ask

How to get the first value in a LINQ list search?

You want to search an object in object list. This will help you in getting the first or default value in your Linq List search. Show activity on this post. You can use FirstOfDefault with the Where LINQ extension to get a MessageAction class from the IEnumerable.

What is LINQ except in SQL?

Linq Except is the C# equivalent of using “Where X Not In” in SQL. There are all sorts of reasons you might want the values from one list that aren’t in another, for instance:

Do LINQ methods preserve the Order of the items in the lists?

I wasn’t sure, so I tried it, and yes, these Linq methods do preserve the order of the items in the Lists: Hopefully this has given you a good introduction to performing set based operations in C# with Linq. In particular using Except to find the items in one list that aren’t in another.

Which are the equivalents of LINQ’s take and Skip methods?

which are the equivalents of: LINQ’s Take and Skip methods are very useful for paging data, or limiting the amount you process, and TakeWhile and SkipWhile come in handy from time to time as well (TakeWhile can be a good way of checking for user cancellation). Take and Skip can be implemented using the itertools islice function.


2 Answers

Just for reference, here is a table of some old .NET 2 style List<> instance methods, and their equivalent extension methods in Linq:

METHOD IN List<>                              METHOD IN Linq
------------------------------------------------------------------------------------------

list.Contains(item)                           query.Contains(item)

list.Exists(x => x.IsInteresting())           query.Any(x => x.IsInteresting())
list.TrueForAll(x => x.IsInteresting())       query.All(x => x.IsInteresting())

list.Find(x => x.IsInteresting())             query.FirstOrDefault(x => x.IsInteresting())
list.FindLast(x => x.IsInteresting())         query.LastOrDefault(x => x.IsInteresting())

list.FindAll(x => x.IsInteresting())          query.Where(x => x.IsInteresting())

list.ConvertAll(x => x.ProjectToSomething())  query.Select(x => x.ProjectToSomething())

Of course some of them are not entirely equivalent. In particular Linq's Where and Select use deferred execution, while FindAll and ConvertAll of List<> will execute immediately and return a reference to a new List<> instance.

FindLast will often be faster than LastOrDefault because FindLast actually searches starting from the end of the List<>. On the other hand LastOrDefault(predicate) always runs through the entire sequence (starting from the first item), and only then returns the most "recent" match.

like image 105
Jeppe Stig Nielsen Avatar answered Oct 20 '22 23:10

Jeppe Stig Nielsen


The LINQ equivelent would be to use FirstOrDefault:

var match = myEnumerable.FirstOrDefault(value => value.Aaa == aaa && value.Bbb == bbb);
like image 10
Reed Copsey Avatar answered Oct 21 '22 01:10

Reed Copsey