动态LINQ to SQL查询 [英] Dynamic LINQ to SQL queries

查看:77
本文介绍了动态LINQ to SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
在问"高级搜索页"以及如何在LINQ中按3或4个条件进行搜索之前,我是新手.对我的回答如下所示,对我来说是可以理解的.
现在,在Google中搜索动态查询"后,他们引用了如下所示的一些地址,看起来很复杂.
现在我不知道哪个更好,我必须选择哪个,请帮忙.

最好的问候.


1-回答的问题:

您应该通过将它们尽可能多地连接在一起(在数据库中),使搜索表成为一个单一视图.
将视图带入数据层和业务层后,您可以非常轻松地通过LINQ表达式应用条件.
只需使用AND运算符将搜索项分开.
因此,当用户点击搜索按钮时,您应该执行以下操作:

... MySearchView.Where(i => i.Name == NameCriteria.Text& i.Country == DropDownList.SelectedVaule& ... ...);

或者如果您的数据层支持导航,请执行以下操作:

... Country.Where(i => i.Country == DropDownList.SelectedVaule).Students.Where(i => i.Name == NameCriteria.Text)...

(请注意每个步骤中的空结果)

并将结果重定向到网格或类似的东西."

2-如何在运行时使用C#在其网站上轻松地为SQL查询构建动态LINQ.参见http://tomasp.net/blog/dynamic-linq-queries.aspx.

3 -动态LINQ(第1部分:使用LINQ动态查询库),作者:ScottGu's Blog

Hello
I am new to line before I had question about "Advance Search Page" and how I can search by 3 or 4 conditions in LINQ. Answer to me was like below and understandable for me.
Now after making some search in Google about "dynamic query" they have referred to some address like below that seem complicate.
Now I don''t know which is better and which I have to select please help.

Best regards.


1-Answered question:

" You should make your search tables into a one single view by joining them as much as possible(in Database)
After bringing your view into data layer and business layer you can apply the criteria by LINQ expressions very easily.
Just separate search items by AND operators.
So when user clicks on search button you should do something like this :

... MySearchView.Where(i=> i.Name == NameCriteria.Text && i.Country == DropDownList.SelectedVaule && ... );

or if your Data layer supports navigation do something like this :

... Country.Where(i => i.Country == DropDownList.SelectedVaule).Students.Where(i=> i.Name == NameCriteria.Text ) ...

(take care of null result in each step)

and redirect the results to a grid or something like that."

2-how to easily build dynamic LINQ to SQL queries at run-time in C# on his web site. See http://tomasp.net/blog/dynamic-linq-queries.aspx.

3-Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library) by ScottGu''s Blog

推荐答案

您尝试了哪些?
您尝试针对哪种搜索样式自定义您需要他们进行的搜索?您最喜欢哪种方法?

对我来说,一旦您可以回答这些问题,您自己就会知道大部分答案,但这只是一种探索.
Which have you tried?
Which have you tried to customize toward the style of searches you would need them to do? which one of the methods are you most comfortable with?

For me once you can answer these questions you will know most of the answer yourself, but it is a case of exploration.


我尝试了#3,它确实有效.我的经验一点也不差,但是最后我并不需要它的全部功能.我最终手动编写了查询表达式:本地LINQ API [ ^ ]非常简单,可用于我的情况.
I tried the #3, and it works. My experience with it was not bad at all, but in the end I did not need its full power. I ended up composing query expressions manually: the native LINQ API[^] was simple enough to adopt for my case.


这篇关于动态LINQ to SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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