使用实体框架创建动态查询
我想知道使用实体框架和 linq 创建动态查询的最佳方法是什么。
我想创建一个具有许多用于排序和过滤的参数(超过 50 个)的服务。我将从 gui 中获取对象,其中将填写这些对象...并且查询将从单个服务方法执行。
我环顾四周,发现我可以动态创建一个可以在方法末尾执行的字符串。我不太喜欢这种方式。有更好的方法吗?最好是带有编译检查的类型安全?
I would like to know what is the best way of creating dynamic queries with entity framework and linq.
I want to create a service that has many parameters for sorting and filtering (over 50). I will be getting object from gui where these will be filled out... and query will be executed from a single service method.
I looked around And I saw that I could dynamically create a string that can be executed at the end of my method. I don't like this way very much. Is there a better way to do this? Preferably type safe with compile check?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以逐步编写一个
IQueryable
。假设您有一个 FilterDefinition 类,它描述了用户想要如何过滤……那么您可以构建一个如下查询:
You could compose an
IQueryable<T>
step by step. Assuming you have aFilterDefinition
class which describes how the user wants to filter ...... then you could build a query like so:
据我所知,唯一的其他方法是根据您的过滤器值构建 IQueryable。
The only other way that I know of would be to build an IQueryable based on your filter vaues.
我创建了一个通用存储库,它应该可以帮助您。它支持统一的 API 对已知字段和动态字段进行查询和排序:
我不知道您获得的搜索对象/DTO,但您可以轻松创建通用搜索对象/DTO,并可以将其映射到查询对象代码行。我过去在 WCF 服务中使用过它,它对我来说非常有效。
I have created a generic repository which should help you. It supports uniform API to query and sort on both known and dynamic fields:
I do not know about the search object/DTO you're getting but you can easily create a generic search object/DTO and can map it to a Query object in few lines of code. I have used it in past around a WCF service and it has worked very well for me.
您可以考虑使用 WCF 数据服务创建服务并动态创建 URI 来查询实体模型。
You could look into creating the service using WCF Data Services and dynamically create the URI to query your entity model.