I am returning IList from Business layer. But in viewmodel I have to use Find function. One method is to convert IList to List.
But is there anyway to add "Find" method to IList
You can declare the variable as List<T> instead of IList<T> or cast it to List<T> in order to gain access to AddRange .
public bool Contains (T item); Here, item is the object which is to be locate in the List<T>. The value can be null for reference types. Return Value: This method returns True if the item is found in the List<T> otherwise returns False.
Find() would return null when the condition wasn't satisfied.
Well, there are the Linq extension methods .Where
(to fecth all that match) and .FirstOrDefault
(to fetch the first match) or you can write your own extension method against IList like:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With