I would like to call FindLast on a collection which implements IEnumarable, but FindLast is only available for List. What is the best solution?
A:
Use the extension method Last() which is located in the namespace System.Linq.
davcar
2009-01-12 15:29:08
That doesn't apply the predicate...
Marc Gravell
2009-01-12 15:58:21
+1
A:
you can add you collection to a new List by passing it to List<> constructor.
List<MyClass> myList = new List<MyClass>(MyCol);
myList.FindLast....
pablito
2009-01-12 15:30:07
+5
A:
The equivalent to:
var last = list.FindLast(predicate);
is
var last = sequence.Where(predicate).LastOrDefault();
(The latter will have to check all items in the sequence, however...)
Effectively the "Where()" is the Find part, and the "Last()" is the Last part of "FindLast" respectively. Similarly, FindFirst(predicate)
would be map to sequence.Where(predicate).FirstOrDefault()
and FindAll(predicate)
would be sequence.Where(predicate)
.
Jon Skeet
2009-01-12 15:41:34
Depending on the comparison method, length of list and frequency of matches it might be more CPU efficient to:sequence.Reverse.Where(predicate).FirstOrDefault();Not very memory efficient however.
mancaus
2009-01-12 16:51:41
+3
A:
How about with LINQ-to-Objects:
var item = data.LastOrDefault(x=>x.Whatever == "abc"); // etc
If you only have C# 2, you can use a utility method instead:
using System;
using System.Collections.Generic;
static class Program {
static void Main() {
int[] data = { 1, 2, 3, 4, 5, 6 };
int lastOdd = SequenceUtil.Last<int>(
data, delegate(int i) { return (i % 2) == 1; });
}
}
static class SequenceUtil {
public static T Last<T>(IEnumerable<T> data, Predicate<T> predicate) {
T last = default(T);
foreach (T item in data) {
if (predicate(item)) last = item;
}
return last;
}
}
Marc Gravell
2009-01-12 15:46:37
The top bit, maybe - but I thought the 2.0 stuff might be useful, so I kept it separate.
Marc Gravell
2009-01-12 15:57:18
Ah, only saw v1!Handn't even considered that one would go and invest the time to expand on it as you did...BTW for all these Util type things, PowerCollections used to be my hammer back when I was constrained to 2.0 - algorithms.cs is a great read. Bet it's Last() has an opt for Collection!
Ruben Bartelink
2009-01-12 16:18:38