简体   繁体   English

Linq-to-sql用户生成的谓词

[英]Linq-to-sql user generated predicate

Background: I have a huge table many columns and many entries in a db. 背景:我有一个巨大的表,在数据库中有许多列和许多条目。 I have some preset queries that return data but I need the ability to let the user further refine the query (add more filters). 我有一些返回数据的预设查询,但我需要能够让用户进一步优化查询(添加更多过滤器)。

So my question is I have code as follows: 所以我的问题是我的代码如下:

var dbquery = (from a in db.test where id > 100 select a);

This returns a iQueryable. 这将返回一个iQueryable。 Now I can futher filter it (and still have the query run on the db) when i do this 现在我可以进一步过滤它(并且仍然在数据库上运行查询)当我这样做

var dbquery2 = dbquery.Where(b => b.quanity > 20);

But what I really want is for the user to be able to input the filters and have it still run on the db. 但我真正想要的是让用户能够输入过滤器并让它仍在db上运行。 I have lookat at PropertyDescriptors but they always say there is no approriate linq to sql translation. 我已经看过PropertyDescriptors,但他们总是说没有合适的linq到sql的翻译。 How can I do something like this: 我该怎么做这样的事情:

var dbqueryuser = dbquery.where(c => c.(user specified field) > 20);

Do I need to do custom generated SQL statements or can I somehow generate valid 我是否需要自定义生成的SQL语句,或者我可以以某种方式生成有效的

Expression<Func<test,bool>> 

statements using reflection? 使用反射的陈述?

Note: This is building on a previous question I asked here Linq to custom sql 注意:这是基于我在这里问Linq自定义sql的上一个问题

I basicly need to take the filter list but get the queries to run on the db (as the db returns a lot of data and I want to get as much filtered at the db side rather than user side) 我基本上需要获取过滤器列表但是让查询在db上运行(因为db返回了大量数据,我想在db端而不是用户端进行过滤)

You have to generate a custom Expression , just for example (it won't compile): 你必须生成一个自定义Expression ,例如(它不会编译):

var param = Expression.Parameter(typeof(Entity));
var property = Expression.PropertyOrField(param, propertyName);
var greater = Expression.GreaterThan(property, Expression.Constant(20));
var lambda = (Expression<Func<Entity, bool>>)Expression.Lambda(greater, param);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM