使用复杂对象的c# Lambda表达式
本文关键字:Lambda 表达式 对象 复杂 | 更新日期: 2023-09-27 18:10:31
如何在具有列表PaymentFields
对象的Payment
对象列表上使用Lambda表达式?以下是我目前的资料
var list = paymentList.Where(payment => payment.PaymentFields.Any(field => field.FieldName == "ItemA" && field.FieldValue == "50");
这给了我以ItemA作为字段名,50作为字段值的支付。但是,我想比较两个PaymentFields,像这样…
Where FieldName == "ItemA" && FieldValue = "50" && FieldName ItemA < FieldName ItemB
我该怎么做呢?
有两个对象:
public class Payment
{
public int Id { set; get; }
public string Name { set; get; }
public List<PaymentFields> PaymentFields { set; get; }
}
public class PaymentFields
{
public string FieldName { set; get; }
public string FieldValue { set; get; }
}
下面是一个示例对象:
var payment = new Payment()
{
Id = 1,
Name = "Test",
PaymentFields = new List<PaymentFields>()
{
new PaymentFields()
{
FieldName = "ItemA",
FieldValue = "20"
},
new PaymentFields()
{
FieldName = "ItemB",
FieldValue = "50"
}
}
};
谢谢你的帮助!
如果你绝对确定会有" itemema "answers"ItemB",那么这就可以了。
如果缺少" itemema "或"ItemB",则抛出异常。
var list = paymentList
.Where(payment => payment.PaymentFields.Any(field => field.FieldName == "ItemA" && field.FieldValue == "50")
.Where(payment => payment.PaymentFields.First(field => field.FieldName == "ItemA").FieldValue
<
payment.PaymentFields.First(field => field.FieldName == "ItemB").FieldValue)
);