for (int r = 2; r < 100; r++)
            {
                List<int> dq = new List<int>(3);
                for (int c = 2; c <= 4; c++)
                {
                    dq.Add(ListGrid.Cell(r, c).IntegerValue);
                }
                dq.Distinct();//去除重复元素                if (dq.Count == 2) //判断剩下的元素个数是否是2个
            }
请问,错在哪里?如何统计元素个数

解决方案 »

  1.   

    dq.Distinct();//去除重复元素
    =》
    dq=dq.Distinct().ToList();//去除重复元素
      

  2.   


    if (dq.count == 2) 是否正确呢?
      

  3.   

    楼上两位在搞笑吗?dq.Distinct();//去除重复元素  这个只是在执行函数而已,你还得用变量来接值才行.dq = dq.Distinct();看定义:'Declaration
    <ExtensionAttribute> _
    Public Shared Function Distinct(Of TSource) ( _
    source As IEnumerable(Of TSource) _
    ) As IEnumerable(Of TSource)
      

  4.   

    楼上两位在搞笑吗?dq.Distinct();//去除重复元素  这个只是在执行函数而已,你还得用变量来接值才行.dq = dq.Distinct();看定义:'Declaration
    <ExtensionAttribute> _
    Public Shared Function Distinct(Of TSource) ( _
    source As IEnumerable(Of TSource) _
    ) As IEnumerable(Of TSource)
      

  5.   


    哪里在搞笑?已经指出来了,并且你的写法有问题 要ToList一下,类型对应
      

  6.   

               for (int r = 2; r < DataGrid.Rows; r++)
                {
                    List<int> dq = new List<int>(3);
                    for (int c = 2; c <= 4; c++)
                    {
                        dq.Add(ListGrid.Cell(r, c).IntegerValue);
                    }
                    dq.Distinct().ToList();
                    if (dq.Count() == 2)
                    {
                        ListGrid.Cell(r, 15).Text = "0"; 
                    }              
                }
    没有效果,没能在制定位置 填入 0
      

  7.   

    也可以这样
    IEnumerable<int> distinctdq = dq.Distinct();
    if (dq.Count() == 2) //判断剩下的元素个数是否是2个
      

  8.   

    dq.Distinct().Count() == 2
    这样就好了嘛,反正Distinct结果又不需要。
      

  9.   

    是dq= dq.Distinct().ToList();
    而不是dq.Distinct().ToList();
    这样可能,永远进不了判断dq.Distinct().ToList();这和你上面的没什么区别,只是调用了一个方法,对下面没有任何影响你要重新赋值才行
      

  10.   

    for (int r = 2; r < DataGrid.Rows; r++)
                {
                    List<int> dq = new List<int>(3);
                    for (int c = 2; c <= 4; c++)
                    {
                        dq.Add(ListGrid.Cell(r, c).IntegerValue);
                    }
                    dq = dq.Distinct().ToList();
                    if (dq.Count() == 2)
                    {
                        ListGrid.Cell(r, 15).Text = "0"; 
                    }              
                }
      

  11.   


    为何分开写,就不行呢?
    dq.distinct();
    dq.count() == 2
      

  12.   


     string test_a = "ssss";
                    test_a.ToUpper();
                    //那么此时你认为test_a是大写还是小写呢?
      

  13.   

    The Distinct<TSource>(IEnumerable<TSource>) method returns an unordered sequence that contains no duplicate values. It uses the default equality comparer, Default, to compare values.