[c#]动态 Linq"在哪里声明"与关系

发布时间: 2017/2/25 5:33:17
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图生成使用动态 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();

这伟大的工作然后出版物列表提供的产品。现在,问题是。我怎样才能字符串语句以获得结果的基础,将关系到产品使用动态 Linq 和字符串声明?

就像这样︰

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

解决方法 1:

经过大量的研究和尝试事物,这是轻松和友好的方式,使用相同的动态 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();
赞助商