我想在结算时过滤附件:
var billing = db.Billings
.Include(b => b.Client)
.Include(b => b.Attachments.Where(a => a.WorkflowStateID == workflowStateID))
.Where(b => b.BillingID == id)
.FirstOrDefault();
结算实体:
public partial class Billing
{
public Billing()
{
this.Attachments = new HashSet<Attachment>();
}
public long BillingID { get; set; }
public int ClientID { get; set; }
public virtual ICollection<Attachment> Attachments { get; set; }
public virtual Client Client { get; set; }
}
但是它会出错
The Include path expression must refer to a navigation property defined on the type.
Use dotted paths for reference navigation properties and the Select operator for
collection navigation properties
如何在include上使用where
子句?
我想要实现的是如果我在查询sql中翻译:
select *
from Billing b
inner join Client c on b.ClientID = c.ClientID
inner join (select * from Attachment a where a.WorkflowStateID = @workflowStateID) t on b.BillingID = t.BillingID
where b.BillingID = @billingID
答案 0 :(得分:3)
如上所述,不允许在Include方法中使用Where
。据我所知,不可能过滤这样的导航属性。你可以做的是使用投影
var billing = db.Billings
.Where(b => b.BillingID == id)
.Select(b => new {
Billing = b,
BillingClient = b.Client
FilteredAttachments = b.Attachments.Where(a => a.WorkflowStateID == workflowStateID)
})
.FirstOrDefault();