gpt4 book ai didi

linq - 动态 LINQ : Comparing Nested Data With Parent Property

转载 作者:行者123 更新时间:2023-12-02 04:51:26 25 4
gpt4 key购买 nike

我有一个具有以下结构的类:

 public class BestWayContext
{
public Preference Preference { get; set; }
public DateTime DueDate { get; set; }
public List<ServiceRate> ServiceRate { get; set; }
}

public class ServiceRate
{
public int Id { get; set; }
public string Carrier { get; set; }
public string Service { get; set; }
public decimal Rate { get; set; }
public DateTime DeliveryDate { get; set; }
}

我有动态 linq 表达式字符串

“Preference != null && ServiceRate.Any(Carrier == Preference.Carrier)”

我想在 Dynamic LINQ 中按如下方式转换上述字符串:

  var expression = System.Linq.Dynamic.DynamicExpression.ParseLambda<BestWayContext, bool>(condition, null).Compile();

但它显示以下错误:

enter image description here

请指正我做错了什么?

最佳答案

看起来你想做这样的事情:

var bwc = new BestWayContext
{
Preference = new Preference { Carrier = "test" },
DueDate = DateTime.Now,
ServiceRate = new List<ServiceRate>
{
new ServiceRate
{
Carrier = "test",
DeliveryDate = DateTime.Now,
Id = 2,
Rate = 100,
Service = "testService"
}
}
};

string condition = "Preference != null && ServiceRate.Any(Carrier == @0)";
var expression = System.Linq.Dynamic.DynamicExpression.ParseLambda<BestWayContext, bool>(condition, bwc.Preference.Carrier).Compile();
bool res = expression(bwc); // true
bwc.ServiceRate.First().Carrier = "test1"; // just for testing this -> there is only one so I've used first
res = expression(bwc); // false

关于linq - 动态 LINQ : Comparing Nested Data With Parent Property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27729395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com