How to add "Find" function to IList
Asked Answered
N

5

5

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

Nicodemus answered 14/11, 2010 at 2:59 Comment(1)
Just create your own IFindableList : IList implementation that includes the Find functionality. Since you control your own BL code you can return this FindableListTroxler
H
8

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);
    }
}
Hacking answered 14/11, 2010 at 3:12 Comment(4)
If I add this file to my .cs project, Can I access IListVar.FindFirst() ?Nicodemus
Yes, as long as IListExtensions is in scope you will be able to call a FindFirst method against any IList<T>.Hacking
What is ToList() method in Linq ?..Can I use thisNicodemus
ToList will turn an IEnumerable<T> into a List<T>.Hacking
O
3

you can use Where method

list.Where(predicate).First()
Occupation answered 14/11, 2010 at 3:13 Comment(2)
list.First(predicate) will perform better than list.Where(predicate).First() as the second will enumerate all the list looking for more than one occurrenceBlaney
First() will raise an exception if item is not found, you want FirstOrDefault() to be the same behaviour as Find()Bassett
N
2

Can you use the IndexOf method?

http://msdn.microsoft.com/en-us/library/3w0148af.aspx

Nicotinism answered 14/11, 2010 at 3:1 Comment(4)
If you're using .net 3.5 or 5 you can use the .Select and get all elements in the IList that satisfy a condition msdn.microsoft.com/en-us/library/bb548891.aspxNicotinism
Is there anyway to Add these functions to IListNicodemus
I think that because an IList is IEnumerable that it already has these extension methods, have a look at msdn.microsoft.com/en-us/library/5y536ey6.aspx .Nicotinism
Although, this is an IList<T>, you might have to cast to your specific type.Nicotinism
B
0

very simple, just you need

casting step

var myModelasList= IListReturnedViewModel as List<ViewModelObject>;
//now you can use list feaures like Find Func.
myModelasList.Find((t => t.SomeFiald== currentState && t.IsSomting == somesymbol);
Blanc answered 15/11, 2015 at 14:31 Comment(0)
B
0

I wrote an extension method to do the casting for me.

public static T Find<T>(this IList<T> ilist, Predicate<T> match)
{
  if (ilist is List<T> list)
  {
    return list.Find(match);
  }
  else if (ilist is T[] array)
  {
    return Array.Find(array, match);
  }
  else
  {
    return ilist.FirstOrDefault(i => match(i));
  }
}
Bassett answered 8/12, 2020 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.