LIST 去重复 list1 a2 b3 c4 d1 e2 f怎样去掉重复的 1 2变为1 ae2 bf3 c4 d 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一个比较笨的方法,list按照第一列排序之后,循环跟上一项对比,然后第一列的值相同,则把第二列的值添加到前一列。 想偷懒就linq 的gruop不想想偷懒 就自己就用循环+字典类 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)) }); 循环+字典类 解法 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; } } 用Linq + DISTINCT函数过滤掉 http://blog.csdn.net/fangxinggood/article/details/6187043 其实只需要LINQ GroupBy一下,把id号相同的合并就行了 其实4L的代码就是Linq GroupBy的实现的简化变体。 vs2008调用string.Join需要转换成数组:p.SelectMany(c=>c.Value).ToArray() 曹版主,我试过了呢。都不可以,我用是的3.5 SP1 VS2008 linq 的groupby,然后用模型类 什么错?p.Select(v => v.Value) 呢? 都不可以呢。类型转换不了,主要想学学你们的是不是可以。无法从“System.Collections.Generic.IEnumerable<char>”转换为“string[]”因为p.SelectMany()返回是的一个IEnumerable<char>类型,而string.Join需要接收string[]所以给报错了 这个手段其实有很多,并不一定就是非要string.format,比如我们还可以直接new一个stringnew string(p.SelectMany(c=>c.Value).ToArray()) p.Select(v => v.Value) 为什么不可以? 开始忘记ToList()了,所以没看到结果,现在看到了,谢谢帮助!! 但凡是遇到处理数组,集合问题的,就往linq想,没错. List<T> lists=new List<T>();lists=lists.Select(p=>v.Value).toList();的确可以得到删选后的集合! 我最开始这样lists=lists.Select(p=>v.Value) as List<T>;返回回来的始终是个Null hashtable在添加的时候判断用lambda表达式linq 隔年流水号自动归零有什么好的处理办法? Lable 显示的内容不分段落。。 写了个验证表单的Ajax,不懂怎么调用 如何在打开网页时禁止VS2005调试? VS2005怎样样进行JAVASCRIPT调试 求一个人才网的项目文档~ HTML的问题,如何让表格保持一定的长宽比 asp.net中,怎样动态添加模块? DataGrid(二) 在.net中,如何提交到另一页面后(用form),用table控制输出 求大神解释问题,关于获取客户端唯一信息 MVC下,使用Ajax无刷新来关联2个combox,但是我想修combox内容后,把更新结果及时显示,请问怎么做呢?
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)) });
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;
}
}
用Linq + DISTINCT函数过滤掉
p.SelectMany(c=>c.Value).ToArray()
类型转换不了,主要想学学你们的是不是可以。
无法从“System.Collections.Generic.IEnumerable<char>”转换为“string[]”因为p.SelectMany()返回是的一个IEnumerable<char>类型,而string.Join需要接收string[]所以给报错了
这个手段其实有很多,并不一定就是非要string.format,比如我们还可以直接new一个stringnew string(p.SelectMany(c=>c.Value).ToArray())
lists=lists.Select(p=>v.Value).toList();
的确可以得到删选后的集合!
返回回来的始终是个Null