由属性值组合对象 [英] Grouping objects by property value

查看:112
本文介绍了由属性值组合对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点麻烦搞清楚了这一点。我有一个对象具有两个属性的列表:别名

I'm having a bit of trouble figuring this out. I have a list of objects with two properties: Alias and Value

我需要证明这些对象所在的别名属性由字符串值分组列表。像这样的:

I need to show a list of these objects where the Alias property is grouped by the Value string value. Like this:

MyAlias1, MyAlias2, MyAlias3
     - Value string which is the same for above three aliases

MyAlias4, MyAlias5
     - Value string which is the same for above three aliases

我至今是:

var groups = lst.GroupBy(x => new { x.Alias, x.Value });
foreach(var group in groups)
{
    @group.Value
}

但我不知道该怎么从那里做,实际显示他们作为分组的项目。

But I'm not sure what to do from there, to actually show them as grouped items.

任何帮助是极大AP preciated!

Any help is greatly appreciated!

推荐答案

我觉得你的价值需要组,然后每个组可以打印出你所需要的:

I think you need to group by the value, then for each group you can print out what you need:

@{ var groups = lst.GroupBy(x => x.Value); }

@foreach (var group in groups)
{
    var aliasString = String.Join(",", group.Select(x => x.Alias));
    <p>@aliasString</p>
    <p>- @group.Key</p>
}

这篇关于由属性值组合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆