gpt4 book ai didi

c# - 具有关系的动态 Linq "Where statement"

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

我正在尝试使用 Dynamic Linq 和我自己计算的 where 字符串语句来构建查询。例如:

List<Publication> results = new List<Publication>();

// Just an example, previously calculated dynamically
string filterQuery = "(Id = 1 and Number = 2)";

IQueryable<Publication> query = db.Publications.Include(i => i.Product);
query = query.Where(filterQuery);

results = query.OrderBy(orderQuery).ToList();

效果很好,我得到了一份包含产品的出版物列表。现在……问题是。如何使用 Dynamic Linq 和字符串语句创建字符串语句以根据与 Product 的关系获取结果?

类似于:

string filterQuery = "(Id = 1 and Number = 2 and Products.Id = 1)"

最佳答案

经过大量研究和尝试,这是一种使用相同 Dynamic Linq 库的简单友好的方法:

List<Publication> results = new List<Publication>();

// Just an example, previously calculated dynamically
string filterQuery = "(Id = 1 and Number = 2)";
string filterQueryChildren = "Products.Any(Id == 1)"

IQueryable<Publication> query = db.Publications.Include(i => i.Product).Where(filterQueryChildren);

query = query.Where(filterQuery);

results = query.OrderBy(orderQuery).ToList();

关于c# - 具有关系的动态 Linq "Where statement",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40954228/

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