【问题标题】:Adding another table to a grouped linq query将另一个表添加到分组的 linq 查询
【发布时间】:2016-11-03 16:07:17
【问题描述】:

我有两张桌子

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

我想将人名、出生日期和状态添加到选择列表中。

我创建了一个类

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

【问题讨论】:

    标签: c# entity-framework linq


    【解决方案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

    【讨论】:

    • 如何按 LineNo 排序以确保行的正确顺序?
    • 我收到错误Cannot implicitly convert type '<anonymous type: int PersonID, string PersonName, System.DateTime DOB, string Status>' to 'int'
    • 这正是我所做的 - 我使用了第一个示例,但它给了我上述错误
    • 哪一部分抛出异常?如果您删除第二个选择,第一部分是否有效?
    • 看起来第一个示例有效。如何添加 orderby 以按 LineNo 对记录进行排序以确保行的顺序正确?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2022-08-17
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多