优化 LINQ 查询以与 jQuery 自动完成配合使用
我正在构建一个 HTTPHandler,它将提供纯文本以与 jQuery Autocomplete 一起使用。我现在可以使用它,除了当我插入第一段文本时它不会将我带到字母表的正确部分。
示例:如果我输入 Ne
我的下拉菜单会返回
恩拉巴马
阿肯色州
请注意 Ne
中的“N”和“Alabama”中的“labama”
当我键入第三个字符 New
时,jQuery 返回结果。
我当前的代码看起来像这样
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
''# the page ContentType is plain text
HttpContext.Current.Response.ContentType = "text/plain"
''# store the querystring as a variable'
Dim qs As Nullable(Of Integer) = Integer.TryParse(HttpContext.Current.Request.QueryString("ID"), Nothing)
''# use the RegionsDataContext'
Using RegionDC As New DAL.RegionsDataContext
''# create a (q)uery variable'
Dim q As Object
''# if the querystring PID is not blank'
''# then we want to return results based on the PID'
If Not qs Is Nothing Then
''# that fit within the Parent ID'
q = (From r In RegionDC.bt_Regions _
Where r.PID = qs _
Select r.Region).ToArray
''# now we loop through the array'
''# and write out the ressults'
For Each item In q
HttpContext.Current.Response.Write(item & vbCrLf)
Next
End If
End Using
End Sub
所以我现在所处的事实是我偶然发现了“Part" 自动完成方法的部分,我应该只返回该部分中包含的信息。
我的问题是,如何在我的 HTTPHandler 中实现这个概念,而不需要对每个字符更改执行新的 SQLQuery? IE:我对 QueryString("ID") 执行 SQL 查询,然后在每次加载相同 ID 时,我们只过滤“部分”。
http://www.example.com/ReturnRegions .axd?ID=[someID]&Part=[字符串]
I'm working on building an HTTPHandler that will serve up plain text for use with jQuery Autocomplete. I have it working now except for when I insert the first bit of text it does not take me to the right portion of the alphabet.
Example: If I enter Ne
my drop down returns
Nlabama
Arkansas
Notice the "N" from Ne
and the "labama" from "Alabama"
As I type the third character New
, then the jQuery returns the "N" section of the results.
My current code looks like this
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
''# the page ContentType is plain text
HttpContext.Current.Response.ContentType = "text/plain"
''# store the querystring as a variable'
Dim qs As Nullable(Of Integer) = Integer.TryParse(HttpContext.Current.Request.QueryString("ID"), Nothing)
''# use the RegionsDataContext'
Using RegionDC As New DAL.RegionsDataContext
''# create a (q)uery variable'
Dim q As Object
''# if the querystring PID is not blank'
''# then we want to return results based on the PID'
If Not qs Is Nothing Then
''# that fit within the Parent ID'
q = (From r In RegionDC.bt_Regions _
Where r.PID = qs _
Select r.Region).ToArray
''# now we loop through the array'
''# and write out the ressults'
For Each item In q
HttpContext.Current.Response.Write(item & vbCrLf)
Next
End If
End Using
End Sub
So where I'm at now is the fact that I stumbled on the "Part" portion of the Autocomplete method whereby I should only return information that is contained within the Part.
My question is, how would I implement this concept into my HTTPHandler without doing a fresh SQLQuery on every character change? IE: I do the SQL Query on the QueryString("ID"), and then on every subsequent load of the same ID, we just filter down the "Part".
http://www.example.com/ReturnRegions.axd?ID=[someID]&Part=[string]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需从查询字符串中获取
part
并在Where
子句中使用它即可仅过滤以其开头且具有适当 ID 的区域。ToArray 不是必需的
Just grab the
part
from the querystring and use it in theWhere
clause to filter only Regions that start with it AND have the appropriate ID.ToArray is not necessary
我更改了 jQuery 库以使用 jQuery-UI,然后使用 Singleton 对象构建了 iHttpHandler,以避免在每次页面加载时都访问数据库。 这里的答案似乎是对我来说工作得很好。
I changed my jQuery library to use the jQuery-UI and then built my iHttpHandler using a Singleton object in order to avoid hitting the database on every page load. The answer here seems to be working well for me.