List<int> L = new List<int>(){1,2,3,4,5,6,7,8,9,1,6,9};  // return 1  6 9 
            //我想从L 中返回 元素出现次数不为1(即元素出现次数>1) 的元素到列表,如何使用Linq ?

解决方案 »

  1.   

    int[] ary = new int[] { 1, 3, 3, 4, 5, 4 };
      var q = from x in ary
      group x by x into Y where Y.Count()>1  
      select {Y.Key,Y.Count()};
     
      

  2.   

     List<int> ary = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 6, 9 };
                var q = from x in ary
                        group x by x into Y
                        where Y.Count() > 1
                        select Y.Key;
                foreach (var p in q)
                {
                    Console.WriteLine(p);
                }     
      

  3.   

    嗯,谢谢wuyq11,我在想是不是还可以使用LIST的方法解决问题。
    比如:L.Select(a => ...我不会写
      

  4.   

    int[] ary = new int[] { 1, 3, 3, 4, 5, 4 };
    int[] result =  ary.Where(x => ary.Where(y => y == x).Count() > 1).ToArray();
      

  5.   

    如果个数超过1个的,你只需要显示一次。
    int[] result = ary.Where(x => ary.Where(y => y == x).Count() > 1).Distinct().ToArray();
      

  6.   


        List<int> L = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 6, 9 };
                var q2 = from a in L
                         group L by a into b
                         where b.Count() > 1
                         select b; 
      

  7.   

    或是
    int[] result = ary.GroupBy(i => i).Where(y => y.Count() > 1).Select(g => g.Key).ToArray();
      

  8.   

                //IEnumerable<IGrouping<int, Cp>> V = from cp in ((frmMain)this.Owner).SsqCp group cp by cp.QiAc into g orderby g.Key select g;
                //List<int> L = V.Where(a => a.Count() != 1).Select(b => b.Key).ToList();            //List<int> L = (from cp in ((frmMain)this.Owner).SsqCp select cp.QiAc).ToList();
                //L = L.GroupBy(a => a).Where(a => a.Count() > 1).Select(c => c.Key).Distinct().ToList();
                //L.Sort();            List<int> L = (from cp in ((frmMain)this.Owner).SsqCp select cp.QiAc).ToList();
                L = (from a in L group a by a into g where g.Count() > 1 select g.Key).ToList();
                L.Sort();            int n = 0;
                foreach (int item in L)
                {
                    n = n + 1;
                    Console.WriteLine(n + "   " + item);
                }