本文介绍了如何从列表C#普通物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从列表检索常见的元素,要显示在下面的格式。
我有一个列表<性> 的属性类如下:
公共类属性{ 公共财产(){} 公共字符串名称{;组; } 公共字符串值{获得;组; }}列表值如下:
名称值---------------萨姆 - > 1萨姆 - > 2萨姆 - >五迈克 - > 2迈克 - > 3预期结果
我想,以显示与逗号分隔值普通物品,如下所示:
名称值-------------------萨姆 - > 1,2,5迈克 - > 2,3解决方案
的组合 GROUPBY 和的string.join 可以这样做:
列表<性>名单=新名单,LT;性>();VAR的结果= list.GroupBy(R = GT; r.Name) 。选择(R = GT;新 { 名称= r.Key, 值=的string.join(,,R.SELECT(叔= GT; t.Value)) });I want to retrieve common element from list and want to show in below format.
I have a List<Property>, the Property class is as follows:
public class Property { public Property(){} public string Name { get; set; } public string Value { get; set; } }Value of list are as below:
Name Value --------------- Sam --> 1 Sam --> 2 Sam --> 5 mike --> 2 mike --> 3Expected result
I wanted to display common items with comma separated values as shown below:
Name Value ------------------- Sam --> 1, 2, 5 mike --> 2, 3解决方案
A combination of GroupBy and string.Join could do:
List<Property> list = new List<Property>(); var result = list.GroupBy(r => r.Name) .Select(r => new { Name = r.Key, Values = string.Join(",", r.Select(t => t.Value)) });
更多推荐
如何从列表C#普通物品
发布评论