我有这个linq代码,我需要将其转换为相同的SQL,以便我可以直接查询数据库...当它变得复杂时我会陷入困境。有人可以帮忙吗?
LINQ的
_db.BatchPaymentSplits
.Where(bps => bps.YearSetupId == i.YearSetupId)
.Where(bps => bps.CustomerIdEntered != null)
.Where(bps => _db.BatchPayments
.Where(bp => _db.Batches.Where(b => b.BatchTypeId.Equals("T"))
.Select(b => b.BatchId)
.Contains(bp.BatchId)
)
.Select(bp => bp.BatchPaymentId).Contains(bps.BatchPaymentId)
)
到目前为止SQL
SELECT * FROM BatchPaymentSplit
WHERE YearSetupId = 1
AND CustomerIdEntered IS NOT NULL
答案 0 :(得分:1)
我不能说我认为LINQ或结果SQL是表达此查询的最佳方式(我应该使用Join
),但这是我的直译:
SELECT *
FROM BatchPaymentSplits bps
WHERE bps.YearSetupId = i.YearSetupId AND
bps.CustomerIdEntered IS NOT NULL AND
EXISTS (SELECT * FROM BatchPayments bp
WHERE EXISTS (SELECT * FROM Batches b
WHERE b.BatchTypeId = 'T' AND
b.BatchId = bp.BatchId) AND
bp.BatchPaymentId = bps.BatchPaymentId)
您可以在Contains
/ IEnumerable
作为IQueryable
查询时使用EXISTS
表达式翻译=
。