none
怎么用Lambda找到泛型集合指定整型属性的中不存在的最小值? RRS feed

  • 问题

  • 找到一个泛型集合中某个整型类型的属性中不存在的的最小值,设一个字段MinNum,这个字段用于开始遍历的开始值,集合不一定有序。

    假若集合泛型是int,其内部有四个元素,28,18,20,21,设置MinNum为18,从18开始,18存在于集合中,而18自增1位的下一位是19,但19不在集合内,所以19是集合里不存在的最小值。这类过程怎么用lambda实现。







    2019年9月19日 4:07

全部回复

  • Hi,

    你的描述的大意是什么?我不太明白你的表述。为什么最小值是12?你前面并没有提到12。还有,“某个整型类型的属性中不存在的的最小值”这句话是什么意思?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月19日 5:30
    版主
  • Hi,

    你的描述的大意是什么?我不太明白你的表述。为什么最小值是12?你前面并没有提到12。还有,“某个整型类型的属性中不存在的的最小值”这句话是什么意思?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    已经补充
    2019年9月19日 5:53
  • Hi,

    你可以先对List进行排序得到升序的有序集合。

        list.Sort((x, y) => x.CompareTo(y));

    然后,通过下面的Lambda表达式获得想要的结果。

        int i = list.Where((int x) => { return !list.Contains(x + 1); }).First() + 1;

    但我不太清楚设置MinNum的作用是什么,你能详细说一下吗?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月19日 6:22
    版主
  • 版主给的方案可行

    如果集合为4325,这个最小值就是不存在(不连续数?)吗?

     var value = (from y in (from x in c
                                    orderby x ascending
                                    select x)
                         where !c.Contains(y + 1)
                         select y).ToList()[0]+1;


    或者

     var value = (from y in (from x in c
                                        orderby x ascending
                                        select x)
                             where c.IndexOf(y) == 0 //c[0]==y
                             where c[1] != y + 1
                             select y).ToList()[0]+1;




    • 已编辑 ARM830 2019年9月19日 13:57
    2019年9月19日 13:43
  • Hi,

    你可以先对List进行排序得到升序的有序集合。

        list.Sort((x, y) => x.CompareTo(y));

    然后,通过下面的Lambda表达式获得想要的结果。

        int i = list.Where((int x) => { return !list.Contains(x + 1); }).First() + 1;

    但我不太清楚设置MinNum的作用是什么,你能详细说一下吗?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

               int MinNum = 2; //假定不存在的最小值
                int[] list = new int[] { 2, 5, 8, 9 }; //出于表达用整型数组,但实际想比较的可能是泛型集合的实体里的某个整型属性
                 while (list.Contains(MinNum))
                {
                    MinNum++; //自增一位,判断自增后的MinNum是是否存在于集合里,如果不存在,则为不存在的最小值
                }
    
    
    //上述代码过程怎么用Lambda表达式缩减?


    2019年9月20日 1:07
  • Hi,

    我想先确认一下你想要的输入输出结果是不是像下面这样一一对应的。

    输入MinNun    输出MinNum
    1                     1
    2                     3
    3                     3
    4                     4
    5                     6
    6                     6
    7                     7
    8                     10
    9                     10
    10                   10

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月20日 1:49
    版主