本文介绍了如何从列表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#普通物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:55