搜索与MVC + LINQ的+ EF页 [英] Search page with MVC + Linq + EF

查看:129
本文介绍了搜索与MVC + LINQ的+ EF页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想实现一个网页,搜索记录的数据库。动作的网址应该是这样〜/邮件/列表/ {属性名} / {}的PropertyValue

So, I want to implement a page that searches the database for records. The url of the action should be something like "~/Mail/List/{PropertyName}/{PropertyValue}.

属性名称和值相应的实体属性名称作为筛选和值是属性值相匹配。问题是,实体包含许多类型的属性。整型,字符串,日期等..

Property name and value are the corresponding entity property name to filter by and the value is the property value to match. The thing is that the entity contains properties of many types. Int, string, date.. etc.

现在的问题是,我该如何实现动态搜索页面或类似的东西来过滤我的查询?

The question is, how do I implement a dynamic search page or something like that to filter my queries?

谢谢!

推荐答案

所有的LINQ运营商预计lambda表达式中传递所以,当你想要做的过滤像其中(x => X [属性名] == [为PropertyValue]),你需要能够构建相应的lambda前pression。通常情况下,当值在编译时是已知的,没有任何问题,你只构造的lambda。

All LINQ operators expects lambdas to be passed in. So when you want to do filtering like Where(x => x.[PropertyName] == [PropertyValue]), you need to be able to construct the corresponding lambda expression. Normally, when values are known at compile time, there's no problem, you'll just construct the lambda.

当你不知道在编译时的值,你必须构造的前pression树手动(即本质上是什么拉姆达前pression制成的)。这可能是一个有点复杂,但也有资料库,帮助你,就像<一个href=\"http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx\"相对=nofollow> 动态LINQ (的这里是另一个很好的教程,EF)。然后,你可以指定你的其中,的条件的作为一个字符串并库的在运行时将它翻译成前pression树或抛出一个异常如果是错误的。

When you don't know the values at compile time, you have to construct expression tree (that is essentially what lambda expression is made of) manually. This can be a bit complicated, but there are libraries to help you, like Dynamic LINQ (here is another good tutorial with EF). You can then specify your Where condition as a string and the library at runtime will translate it into expression tree or throw an exception if it is wrong.

所以,在你的榜样,使用动态LINQ,你可以做你的查询关键词,比如 db.Mails.Where(属性名+== @ 0,为PropertyValue);

So, in your example, using Dynamic LINQ, you can do your query like db.Mails.Where(PropertyName + " == @0", PropertyValue);

这篇关于搜索与MVC + LINQ的+ EF页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆