简体   繁体   English

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

[英]Adding another table to a grouped linq query

I have the two tables 我有两张桌子

Person
__________
PersonID
PersonName
DOB
Status


Notes
__________
NoteID
PersonID
NoteText
Comments
LineNo

here is some sample content 这是一些样本内容

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

and

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

So as an end result I want a Linq query to display something like that 因此,最终结果是我希望Linq查询显示类似的内容

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

I have a working linq query for the Notes table, but would like to include some fields from Persons table as well: 我对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();

I'd like to add Person name, DOB, and Status to the select list. 我想将“人名”,“ DOB”和“状态”添加到选择列表中。

I created a class 我创建了一个班

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; }
}

To my query I added a join and order by clause to order by line numbers. 在查询中,我添加了一个join和order by子句以按行号排序。 But I am not sure how to add fields to the select list in my anonymous object: 但是我不确定如何将字段添加到匿名对象的选择列表中:

 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();

You can group by multiple fields and then access these properties through the .Key property: 您可以按多个字段分组,然后通过.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();

If all these properties are indeed part of the Person class then you can also just GroupJoin : 如果所有这些属性确实是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();

But I suggest that you look into Navigation Properties. 但我建议您研究导航属性。 Then you won't even need the join. 然后,您甚至不需要加入。 Just have an .Include 只要有一个.Include

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

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