list
1 a
2 b
3 c
4 d
1 e
2 f怎样去掉重复的 1 2
变为
1 ae
2 bf
3 c
4 d

解决方案 »

  1.   

    一个比较笨的方法,list按照第一列排序之后,循环跟上一项对比,然后第一列的值相同,则把第二列的值添加到前一列。
      

  2.   

    想偷懒就linq 的gruop不想想偷懒 就自己就用循环+字典类
      

  3.   

    linq的解法
      List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();            list.Add(new KeyValuePair<int, string>(1, "a"));
                list.Add(new KeyValuePair<int, string>(2, "b"));
                list.Add(new KeyValuePair<int, string>(3, "c"));
                list.Add(new KeyValuePair<int, string>(4, "d"));
                list.Add(new KeyValuePair<int, string>(1, "e"));
                list.Add(new KeyValuePair<int, string>(2, "f"));            var res = list.GroupBy(p => p.Key).Select(p => new { p.Key, value = string.Join("", p.SelectMany(c=>c.Value)) });
      

  4.   

    循环+字典类 解法  Dictionary<int, string> res = new Dictionary<int, string>();
                List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();            list.Add(new KeyValuePair<int, string>(1, "a"));
                list.Add(new KeyValuePair<int, string>(2, "b"));
                list.Add(new KeyValuePair<int, string>(3, "c"));
                list.Add(new KeyValuePair<int, string>(4, "d"));
                list.Add(new KeyValuePair<int, string>(1, "e"));
                list.Add(new KeyValuePair<int, string>(2, "f"));
                foreach (var item in list)
                {
                    if (res.ContainsKey(item.Key))
                    {
                        res[item.Key] +=  item.Value;
                    }
                    else
                    {
                        res[item.Key] = item.Value;
                    }                
                }
      

  5.   


    用Linq + DISTINCT函数过滤掉
      

  6.   

    http://blog.csdn.net/fangxinggood/article/details/6187043
      

  7.   

    其实只需要LINQ  GroupBy一下,把id号相同的合并就行了
      

  8.   

    其实4L的代码就是Linq GroupBy的实现的简化变体。
      

  9.   

    vs2008调用string.Join需要转换成数组:
    p.SelectMany(c=>c.Value).ToArray()
      

  10.   

    曹版主,我试过了呢。都不可以,我用是的3.5 SP1 VS2008
      

  11.   

    linq 的groupby,然后用模型类
      

  12.   

    什么错?p.Select(v => v.Value) 呢?
      

  13.   

    都不可以呢。
    类型转换不了,主要想学学你们的是不是可以。
    无法从“System.Collections.Generic.IEnumerable<char>”转换为“string[]”因为p.SelectMany()返回是的一个IEnumerable<char>类型,而string.Join需要接收string[]所以给报错了
      

  14.   


    这个手段其实有很多,并不一定就是非要string.format,比如我们还可以直接new一个stringnew string(p.SelectMany(c=>c.Value).ToArray())
      

  15.   

    p.Select(v => v.Value) 为什么不可以?
      

  16.   

    开始忘记ToList()了,所以没看到结果,现在看到了,谢谢帮助!!
      

  17.   

    但凡是遇到处理数组,集合问题的,就往linq想,没错.
      

  18.   

    List<T> lists=new List<T>();
    lists=lists.Select(p=>v.Value).toList();
    的确可以得到删选后的集合!
      

  19.   

    我最开始这样lists=lists.Select(p=>v.Value) as List<T>;
    返回回来的始终是个Null
      

  20.   

    hashtable在添加的时候判断用lambda表达式linq