gpt4 book ai didi

c# - View 中的行跨度 (mvc4)

转载 作者:行者123 更新时间:2023-11-30 21:58:50 26 4
gpt4 key购买 nike

我将 MVC 4 与 Entity Framework 结合使用。我有以下问题。

我在 View 中的原始数据以这种方式显示:

id | name| email| roles| modules
1|name1| email1| role1| module1
1| name1| email1| role2| module1
1| name1| email1|role3| module1

我需要做的是这样的rowspan

id | name| email| roles| modules
1|name1| email1| role1, role2, role3| module1

我相信在 Controller 中比在 View 中更好。如果是这样,如何使用 Linq 或简单列表来做到这一点?

最佳答案

所有你需要的是 GroupBy:-

IEnumerable<TestForms.Models.v_HmsUnacceptedForms> result = data.GroupBy(x => x.id)
.ToList() //To force run projection in memory.
.Select(x =>
{
var firstRecord = x.FirstOrDefault();
return new TestForms.Models.v_HmsUnacceptedForms
{
id = x.Key,
name = firstRecord != null ? firstRecord.name : String.Empty,
email = firstRecord != null ? firstRecord.email: String.Empty,
roles = String.Join(",",x.Select(z => z.roles),
modules = firstRecord != null ? firstRecord.modules : String.Empty
};
});

在这里,我考虑过对于每个 id,您需要第一个 name,email and modules 列。如果不是这种情况,您可以按多列而不是像 new { x.id, x.name, x.email } 这样的 id 进行分组。

关于c# - View 中的行跨度 (mvc4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29819748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com