How to add Find function in IList

I am returning an IList from the Business level. But in viewmodel I need to use the Find function. One way is to convert IList to List.

But somehow add the Find method to IList

+3
source share
4 answers

Well, there are Linq extension methods .Where(to match all that match) and .FirstOrDefault(to get the first match), or you can write your own extension method against IList, for example:

public static class IListExtensions
{
    public static T FindFirst<T>(this IList<T> source, Func<T, bool> condition)
    {
        foreach(T item in source)
            if(condition(item))
                return item;
        return default(T);
    }
}
+6
source
+2
source

Where

list.Where(predicate).First()
+1

, just you need

var myModelasList= IListReturnedViewModel as List<ViewModelObject>;
//now you can use list feaures like Find Func.
myModelasList.Find((t => t.SomeFiald== currentState && t.IsSomting == somesymbol);
0

Source: https://habr.com/ru/post/1774746/