我正在尝试使用一些linq magic来查询两个不同的数据集,以比较一个数据集上的内部属性,以查看该值是否大于另一个数据集中的属性。我已经推广了类布局并删除了一些属性,希望能让它更清晰。
public class Contract
{
public string ContractId { get; set; }
public IList<Invoice> InvoiceList { get; set; }
public class Invoice
{
public int InvoiceNumber { get; set; }
public decimal CurrentDueAmount { get; set; }
}
}
public class PaymentRequest
{
public IList<ContractList> Contracts { get; set; }
public class ContractList
{
public string ContractId { get; set; }
public IList<InvoiceList> Invoices { get; set; }
}
public class InvoiceList
{
public decimal CurrentDueAmount { get; set; }
}
}
我知道以下LINQ语句不起作用,但它是关于我正在努力实现的一般想法。
from validContract in validContracts
join contract in paymentRequest.Contracts
on validContract.ContractId equals contract.ContractId
from validInvoice in validContract.InvoiceList
join invoice in contract.Invoices
on validInvoice.InvoiceNumber equals invoice.InvoiceNumber
where invoice.CurrentDueAmount > validInvoice.CurrentDueAmount
select "Invoice Number: " + invoice.InvoiceNumber + ", Current Due >= " + validInvoice.CurrentDueAmount;
尝试在第二个连接语句中使用范围变量“contract”时出错。
我希望能够对象currentDueAmounts进行比较。任何想法/重构,方法提取等将不胜感激。
答案 0 :(得分:1)
您需要在加入它们之前将这两个集合分开,如此
var query =
from valid in (
from contract in validContracts
from invoice in contract.InvoiceList
select new { contract, invoice }
)
join request in (
from contract in paymentRequest.Contracts
from invoice in contract.Invoices
select new { contract, invoice }
)
on new { valid.contract.ContractId, valid.invoice.InvoiceNumber }
equals new { request.contract.ContractId, request.invoice.InvoiceNumber }
where request.invoice.CurrentDueAmount > valid.invoice.CurrentDueAmount
select "Invoice Number: " + request.invoice.InvoiceNumber + ", Current Due >= " + valid.invoice.CurrentDueAmount;