Find indices of particular items in the list using linq

后端 未结 2 617
天命终不由人
天命终不由人 2020-12-07 04:01

I have a list of integers from 1 to 20. I want the indices of items which are greater than 10 using linq. Is it possible to do with linq?

Thanks in advance

2条回答
  •  执笔经年
    2020-12-07 04:35

        public static List FindIndexAll(this List src, Predicate value)
        {
            List res = new List();
            var idx = src.FindIndex(x=>x>10);           
            if (idx!=-1) {
            res.Add(idx);
             while (true)
             {
                idx = src.FindIndex(idx+1, x => x > 10);
                if (idx == -1)
                    break;
                res.Add(idx);
             }
            }
            return res;
        }
    

    Usage

            List  test= new List() {1,10,5,2334,34,45,4,4,11};
            var t = test.FindIndexAll(x => x > 10);
    

提交回复
热议问题