我有这个问题:
IQueryable<Comment> comments = _commentRepo.Comments
.Include(c => c.CommentStaff)
.ThenInclude(c => c.StaffOffices)
.Where(c => c.CommentAuditId == auditId)
.OrderByDescending(c => c.CommentDate).Take(3);
StaffOffices是一个多对多的网桥查找POCO,如下所示:
[Table("staff_office")]
public class StaffOffice
{
[Column("staff_office_staff_id")]
public short ID { get; set; }
public Staff Staff { get; set; }
[Column("staff_office_office_id")]
public short OfficeID { get; set; }
public Office Office { get; set; }
}
我需要通过此查询填充Office对象。
所以我正在尝试这个:
正如您所看到的,Office没有出现在Intellisense中,因为我的ThenInclude是关于StaffOffices的。
我怎样才能让它发挥作用? 这是否需要使用流畅的API在AppContext模型中定义?
答案 0 :(得分:1)
答案 1 :(得分:0)
即使它没有在intellisense中显示,它仍然有效。你可以尝试一下。此外,如果您需要员工数据,则必须再次包含StaffOffices。
IQueryable<Comment> comments = _commentRepo.Comments
.Include(c => c.CommentStaff)
.ThenInclude(c => c.StaffOffices)
.ThenInclude(c => c.Office)
.ThenInclude(c => c.StaffOffices)
.ThenInclude(c => c.Staff)
.Where(c => c.CommentAuditId == auditId)
.OrderByDescending(c => c.CommentDate).Take(3);
如果您选择评论人员回购,多对多级别将是第二级。虽然顺序可能很难直接做到。然后,您必须按照注释变量进行排序。
例如:
var comments = _commentStaffRepo.CommentStaff
.Include(c => c.StaffOffices)
.ThenInclude(c => c.Office)
.Include(c => c.StaffOffices)
.ThenInclude(c => c.Staff)
.Include(c => c.Comments)
.Where(c => c.Comments.Any(co => co.CommentAuditId == auditId))
.Select(c => c.Comments);
comments = comments.OrderByDescending(c => c.CommentDate).Take(3);
答案 2 :(得分:0)
感谢Vivek并在上面发表评论。
现在是我的Component Invoke方法:
public IViewComponentResult Invoke(int auditId)
{
IQueryable<Comment> comments = _commentRepo.Comments
.Include(c => c.CommentStaff)
.ThenInclude(c => c.StaffOffices).ThenInclude(so => so.Office)
.Where(c => c.CommentAuditId == auditId)
.OrderByDescending(c => c.CommentDate).Take(3);
CommentVM commentVM = new CommentVM
{
AuditId = auditId,
Comments = comments
};
return View(commentVM);
}
我做了另一个级别ThenInclude:
.ThenInclude(c => c.StaffOffices).ThenInclude(so => so.Office)
以下是Razor中的片段:
<div class="row">
<div class="col-xs-6">
<i>@comment.CommentStaff.FullName </i>
@if (comment.CommentStaff.StaffOffices.Count() > 1)
{
<span>@($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")</span>
}
</div>
<div class="col-xs-6">
@comment.CommentDate
</div>
</div>