LINQ 等效于 WHERE IN 子句
LINQ equivalent of WHERE IN clause
我有 3 个表 Purchase
、PurchaseBill
和 PurchasePayment
。
Purchase
与 PurchaseBill
是一对多关系
PurchaseBill
与 PurchasePayment
是一对多关系
我正在寻找与此 SQL 查询
等效的 Linq(流畅)
SELECT COUNT('') FROM PurchasePayment
WHERE PurchaseBillId IN (
SELECT PurchaseBillId FROM PurchaseBill WHERE PurchaseId = @PurchaseId
)
基本上我想知道某些 @PurchaseId
是否存在任何 PurchasePayment
记录
目前我正在使用
var bills = _context.PurchaseBill.Where(a => a.PurchaseId == obj.Id);
foreach (var bill in bills)
{
var hasPayment = _context.PurchasePayment.Any(a => a.PurchaseBillId == bill.Id);
if (hasPayment)
ctx.AddFailure("Cannot modify paid Purchase.");
}
我试过了
var hasPayment = _context.PurchasePayment.Any(w => bills.Contains(w.PurchaseBillId));
但它不工作。是否可以在 Linq 中不循环执行?
你可以这样实现:
var PurchaseId=1;
var result = _context.PurchasePayment.Where(x=>_context.PurchaseBill.Where(y=>y.PurchaseId==PurchaseId).Select(y=>y.PurchaseBillId).ToList().Contains(x.PurchaseBillId)).Count();
你试过这个吗:
var hasPayment = _context.PurchasePayment.Where(x => _context.PurchaseBill.Where(a => a.PurchaseId == obj.Id).Any(y => y.PurchaseBillId == x.PurchaseBillId)).Any();
我在这里试了一个例子:
https://dotnetfiddle.net/uTs6pd
我同意格特的观点;你应该有一个 PurchaseBill
class 和一个 ICollection<PurchasePayment> PurchasePayments
属性 (链接到多笔付款)和一个 int PurchaseId
属性 (链接回单次购买)并能够执行以下操作:
var hasPayment = context.PurchaseBills.Any(
pb => pb.PurchaseId == obj.Id && pb.PurchasePayments.Any()
);
“是否有带有 PurchaseId blahblah 和任何 PurchasePayment 的采购单?”
或者,如果您打算对每张账单及其付款进行处理,例如:
var bills = context.PurchaseBills.Include(pb => pb.PurchasePayments);
foreach(var pb in bills){
if(pb.PurchasePayments.Any()){
//do something with the bill and its purchase here
}
}
但是如果您要做的只是向错误列表添加一条消息,请不要这样做;为了查看并找到一个可能有付款记录的账单记录,需要下载大量数据——最好通过第一个建议来完成,该建议要求数据库提供一个简单的布尔值——“是否有任何地方为此付款purchaseId?"
我有 3 个表 Purchase
、PurchaseBill
和 PurchasePayment
。
Purchase
与 PurchaseBill
是一对多关系
PurchaseBill
与 PurchasePayment
我正在寻找与此 SQL 查询
等效的 Linq(流畅)SELECT COUNT('') FROM PurchasePayment
WHERE PurchaseBillId IN (
SELECT PurchaseBillId FROM PurchaseBill WHERE PurchaseId = @PurchaseId
)
基本上我想知道某些 @PurchaseId
PurchasePayment
记录
目前我正在使用
var bills = _context.PurchaseBill.Where(a => a.PurchaseId == obj.Id);
foreach (var bill in bills)
{
var hasPayment = _context.PurchasePayment.Any(a => a.PurchaseBillId == bill.Id);
if (hasPayment)
ctx.AddFailure("Cannot modify paid Purchase.");
}
我试过了
var hasPayment = _context.PurchasePayment.Any(w => bills.Contains(w.PurchaseBillId));
但它不工作。是否可以在 Linq 中不循环执行?
你可以这样实现:
var PurchaseId=1;
var result = _context.PurchasePayment.Where(x=>_context.PurchaseBill.Where(y=>y.PurchaseId==PurchaseId).Select(y=>y.PurchaseBillId).ToList().Contains(x.PurchaseBillId)).Count();
你试过这个吗:
var hasPayment = _context.PurchasePayment.Where(x => _context.PurchaseBill.Where(a => a.PurchaseId == obj.Id).Any(y => y.PurchaseBillId == x.PurchaseBillId)).Any();
我在这里试了一个例子: https://dotnetfiddle.net/uTs6pd
我同意格特的观点;你应该有一个 PurchaseBill
class 和一个 ICollection<PurchasePayment> PurchasePayments
属性 (链接到多笔付款)和一个 int PurchaseId
属性 (链接回单次购买)并能够执行以下操作:
var hasPayment = context.PurchaseBills.Any(
pb => pb.PurchaseId == obj.Id && pb.PurchasePayments.Any()
);
“是否有带有 PurchaseId blahblah 和任何 PurchasePayment 的采购单?”
或者,如果您打算对每张账单及其付款进行处理,例如:
var bills = context.PurchaseBills.Include(pb => pb.PurchasePayments);
foreach(var pb in bills){
if(pb.PurchasePayments.Any()){
//do something with the bill and its purchase here
}
}
但是如果您要做的只是向错误列表添加一条消息,请不要这样做;为了查看并找到一个可能有付款记录的账单记录,需要下载大量数据——最好通过第一个建议来完成,该建议要求数据库提供一个简单的布尔值——“是否有任何地方为此付款purchaseId?"