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

标签 c# entity-framework visual-studio linq

我正在尝试使用 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/

相关文章:

c# - 从 C# 使用 native DLL 的输出参数

c# - 如何在单元测试中为并发建模?

.net - 扩展 EF4 SQL 生成

c# - 如何在代码优先的 Entity Framework Fluent API 中指定多列 UNIQUE 约束

c# - 如何在页面中创建 Microsoft Band 自定义图标 - Windows UWP

c# - 我可以在 Windows Phone 7.1 上使用 System.Net.Sockets.TcpListener

C# 绘图,奇怪的行为

entity-framework - C# | Entity Framework |无法从固定大小类型的数组中删除项目

c++ - 将 CMake.txt 转换为 .vcxproj 文件 C++ Visual Studio 项目

c++ - 如何修复 std::vector<int> WidthNumbers = 320, 640, 1280; 中的错误?