简体   繁体   English

视图中的行距(mvc4)

[英]Rowspan in the view (mvc4)

I'm using MVC 4 with the Entity Framework. 我正在使用带有实体框架的MVC 4。 I've the following problem. 我有以下问题。

My raw data in the view is displaying in that way: 我在视图中的原始数据以这种方式显示:

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

What I need to do is rowspan like this 我需要做的是这样的跨度

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

I believe that it's better to make it in the controller than in the view. 我相信最好在控制器中创建它而不是在视图中创建它。 If so, how to do that with the Linq or simple list? 如果是这样,如何使用Linq或简单列表进行操作?

All you need is GroupBy :- 您需要的只是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
                    };
               });

Here, I have considered that for each id, you need the first name,email and modules column. 在这里,我考虑过,对于每个id,您都需要name,email and modules列。 if this is not the case you can group by multiple columns instead of id like new { x.id, x.name, x.email } . 如果不是这种情况,则可以按多个列而不是像new { x.id, x.name, x.email }这样的id进行new { x.id, x.name, x.email }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM