将另一个表添加到分组的linq查询中

时间:2016-11-03 16:07:17

标签: c# entity-framework linq

我有两张桌子

Person
__________
PersonID
PersonName
DOB
Status


Notes
__________
NoteID
PersonID
NoteText
Comments
LineNo

这是一些示例内容

 PersonID   PersonName    DOB          Status
   1    Mark Jacobs   07/07/1961    Active

NoteID  PersonID    NoteText    LineNo
 123       1        Line 1      1
 234       1        Line 2      2
 236       1        Line 3      3

因此,作为最终结果,我希望Linq查询显示类似

的内容
PersonID    PersonName    DOB           Note
   1    Mark Jacobs   07/07/1961        Line 1, Line 2, Line 3

我对Notes表有一个有效的linq查询,但是也希望包含Persons表中的一些字段:

 var result = (from n in db.Notes
                  group n.NoteText by n.PersonID into g
                  select new { 
                      PersonID = g.Key, 
                      Notes = g.ToList()
                  }).AsEnumerable() 
                 .Select(item => new NoteGroupDTO { 
                     PersonID = item.PersonID,
                     Notes = string.Join(", ", item.Notes)
                 }).ToList();

我想将人名,DOB和状态添加到选择列表中。

我创建了一个类

public class PersonNoteDTO
{
    public int PersonID { get; set; }
    public string PersonName { get; set; }
    public DateTime DOB { get; set; }
    public string Status { get; set; }
    public string Notes { get; set; }
}

在我的查询中,我添加了一个join和order by子句来按行号排序。但我不确定如何在匿名对象的选择列表中添加字段:

 var result = (from n in db.Notes
                  join p in db.Persons on n.PersonID=p.PersonID
                  orderby n.LineNo
                  group n.NoteText by n.PersonID into g
                  select new { 
                      PersonID = g.Key, 
                      Notes = g.ToList()
                  }).AsEnumerable() 
                 .Select(item => new PersonNoteDTO { 
                     PersonID = item.PersonID,
                     Notes = string.Join(", ", item.Notes)
                 }).ToList();

1 个答案:

答案 0 :(得分:1)

您可以按多个字段进行分组,然后通过.Key属性访问这些属性:

var result = (from n in db.Notes
              join p in db.Persons on n.PersonID equals p.PersonID
              group new { n.NoteText, n.LineNo } by new { n.PersonID, n.PersonName, p.DOB, p.Status } into g
              select new { 
                  PersonID = g.Key.PersonID, 
                  PersonName = g.Key.PersonName, 
                  DOB = g.Key.DOB, 
                  Status = g.Key.Status, 
                  Notes = g.OrderBy(i => i.LineNo).Select(i=> i.NoteText)
              }).AsEnumerable()

             .Select(item => new PersonNoteDTO { 
                 PersonID = item.PersonID,
                 PersonName = item.PersonName,
                 DOB = item.DOB,
                 Status = item.Status,
                 Notes = string.Join(", ", item.Notes)
             }).ToList();

如果所有这些属性确实属于Person类,那么您也可以GroupJoin

var result = (from p in db.Persons
              join n in db.Notes on p.PersonID equals n.PersonID into personNotes
              select new
              {
                  Person = p,
                  Notes = personNotes.OrderBy(pn => pn.LineNo).Select(pn => pn.NoteText)
              }).AsEnumerable()
             .Select(item => new PersonNoteDTO { 
                 PersonID = item.Person.PersonID,
                 PersonName = item.Person.PersonName,
                 DOB = item.Person.DOB,
                 Status = item.Person.Status,
                 Notes = string.Join(", ", item.Notes)
             }).ToList();

但我建议您查看导航属性。那你甚至不需要加入。只需.Include