英文:
How to filter subqueries in Linq and Entity Framework Core
问题
我应该如何筛选子查询?当选择 Payment
实体时,我想选择 Active == true
的项。
表达式:
public async Task<Invoice> InvoiceAsync(Expression<Func<Invoice, bool>> predicate) => await ExecSingleAsync(this.GetDbSet<Invoice>()
.Include(inv => inv.Actions)
.Include(inv => inv.Account)
.Include(inv => inv.Customer)
.Include(inv => inv.Payments.Where(pm => pm.Active == true)) // 在这里筛选 Payments 实体
.Include(inv => inv.Lines.Where(ln => ln.Active == true))
.Where(inv => inv.Active == true)
.Where(predicate));
Invoice.cs:
public class Invoice : DbAssets, IInvoice
{
// ... 其他属性和方法
public IEnumerable<InvoicePayment> Payments { get; set; } = new HashSet<InvoicePayment>();
// ... 其他属性和方法
}
Payment.cs:
public sealed class InvoicePayment : DbAssets, IInvoicePayment
{
// ... 其他属性和方法
public bool Active { get; set; } // 添加 Active 字段
// ... 其他属性和方法
}
这里我添加了 Payments
和 InvoicePayment
类中的 Active
字段筛选。
英文:
How can I filter the subqueries? When selecting Payment
entities, I want to select where Active == true
.
Expression:
public async Task<Invoice> InvoiceAsync(Expression<Func<Invoice, bool>> predicate) => await ExecSingleAsync(this.GetDbSet<Invoice>()
.Include(inv => inv.Actions)
.Include(inv => inv.Account)
.Include(inv => inv.Customer)
.Include(inv => inv.Payments)
.Where(pm => pm.Active)
.Include(inv => inv.Lines)
.Where(ln => ln.Active)
.Where(inv => inv.Active)
.Where(predicate));
Invoice.cs:
public class Invoice : DbAssets, IInvoice
{
public int BillingClientId { get; set; }
public int? CustomerId { get; set; }
public int AccountId { get; set; }
public virtual Customer Customer { get; set; }
public virtual Account Account { get; set; }
public IEnumerable<InvoiceLine> Lines { get; set; } = new HashSet<InvoiceLine>();
public DateTime BillingDate { get; set; }
public DateTime DueDate { get; set; }
public IEnumerable<InvoiceAction> Actions { get; set; } = new HashSet<InvoiceAction>();
public string? Message { get; set; }
public int Nr { get; set; }
public IEnumerable<InvoicePayment> Payments { get; set; } = new HashSet<InvoicePayment>();
[NotMapped]
public decimal Total => this.Lines.Sum(ln => ln.Sum);
[NotMapped]
public decimal TotalPaid => this.Payments.Sum(pm => pm.Amount);
[NotMapped]
public InvoiceStatus Status => this.TotalPaid >= this.Total ? InvoiceStatus.Closed : InvoiceStatus.Open;
[NotMapped]
public decimal Mva => this.Lines.Sum(ln => ln.Mva);
}
Payment.cs:
public sealed class InvoicePayment : DbAssets, IInvoicePayment
{
public int InvoiceId { get; set; }
public DateTime PaymentDate { get; set; }
public decimal Amount { get; set; }
}
Both classes have an Active
field.
答案1
得分: 0
public async Task<Invoice> InvoiceAsync(Expression<Func<Invoice, bool>> predicate) => await ExecSingleAsync(this.GetDbSet<Invoice>()
.Include(inv => inv.Actions)
.Include(inv => inv.Account)
.Include(inv => inv.Customer)
.Include(inv => inv.Payments.Where(p => p.Active))
.Include(inv => inv.Lines)
.Where(inv => inv.Active)
.Where(predicate));
英文:
public async Task<Invoice> InvoiceAsync(Expression<Func<Invoice, bool>> predicate) => await ExecSingleAsync(this.GetDbSet<Invoice>()
.Include(inv => inv.Actions)
.Include(inv => inv.Account)
.Include(inv => inv.Customer)
.Include(inv => inv.Payments.Where(p => p.Active))
.Include(inv => inv.Lines)
.Where(inv => inv.Active)
.Where(predicate));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论