【发布时间】: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