2016-04-27 72 views
0

我有一个像变更记录查看在

public class Test 
{ 
    public string PatientName{get; set;} 
    public string Address{get; set;} 
    public string TestName{get; set;} 
} 

模型在我的表,我有

TestID PatientName Address TestName 
1  yyy   xxxxx Test1 
2  yyy   xxxxx Test2 
3  zzz   aaaaa Test1 
4  zzz   aaaaa Test2 
5  zzz   aaaaa Test3 

当病人可以有很多的测试。
但在我的节目页面我喜欢它显示为

Patient Name  Address    Test Name 
yyy     xxxxx    Test1, Test2 
zzz     aaaaa    Test1, Test2, Test3 

我不能做一个查询,以显示像上面。 在此先感谢

回答

1

您可以使用GroupBy

var tests = db.Tests.GroupBy(m => new { m.PatientName, m.Address }); 

然后,在你看来,你只是做:

@foreach (var patient in tests) 
{ 
    <tr> 
     <td>@patient.Key.PatientName</td> 
     <td>@patient.Key.Address</td> 
     <td>@String.Join(", ", patient.Select(m => m.TestName))</td> 
    </tr> 
}