Get index of lines that match my linq

前端 未结 3 1648
我寻月下人不归
我寻月下人不归 2021-01-25 21:45

I have a linq statement and I would like to know if it is possible to get indicies of lines that match my statement? Here it is:

var result = list3.Where(middle          


        
3条回答
  •  遥遥无期
    2021-01-25 22:23

    Also note that if you want to search for the indicies of items matching a predicate very often, it could be worth writing a very simple extension method:

    public static class IEnumerableExt
    {
        public static IEnumerable FindIndices(this IEnumerable self, Predicate predicate)
        {
            int i = 0;
    
            foreach (var element in self)
            {
                if (predicate(element))
                    yield return i;
    
                ++i;
            }
        }
    }
    

    Which you would call like this:

    var result = list3.FindIndices(x => list4.Any(xx => xx == x.middle.Middle.category1));
    

提交回复
热议问题