简体   繁体   中英

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

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:

 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.

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. 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:

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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