如何从列表C#普通物品

编程入门 行业动态 更新时间:2024-10-25 00:31:48
本文介绍了如何从列表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 --> 3

Expected 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#普通物品

本文发布于:2023-11-30 01:13:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:物品   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!