VB.NET 从 ADO.NET 切换到 LINQ
我对 Linq 非常陌生。我有一个用 VB.NET 2.0 编写的应用程序。效果很好,但我想将此应用程序切换到 Linq。我使用 ADO.NET 将 XML 加载到数据表中。该 XML 文件包含大约 90,000 条记录。然后,我使用 Datatable.Select 对该数据表执行搜索。搜索控件是一个自由格式的文本框。因此,如果用户输入术语,它会立即进行搜索。输入的任何其他术语都会继续限制结果。因此,您可以输入 Bob,或输入 Bob Barker。或者输入 Bob Barker 价格是正确的。输入的条件越多,结果范围就越窄。我将结果绑定到网格视图。
继续前进我需要做什么?从较高的层面来看,我认为我需要:
1) 转到项目属性 -->高级编译器设置并将目标框架从 2.0 更改为 3.5。 2) 添加对System.XML.Linq 的引用,将Imports 语句添加到类中。
所以我不确定在那之后最好的方法是什么。我假设我使用 XDocument.Load,然后我的搜索子例程针对 XDocument 运行。我是否只需对这种重复搜索执行标准 Linq 查询?像这样:
Dim people =
from phonebook in doc.Root.Elements("phonebook")
where phonebook.Element("userid") = "whatever"
select phonebook
关于如何最好地实施有什么建议吗?
I'm VERY new to Linq. I have an application I wrote that is in VB.NET 2.0. Works great, but I'd like to switch this application to Linq. I use ADO.NET to load XML into a datatable. The XML file has about 90,000 records in it. I then use the Datatable.Select to perform searches against that Datatable. The search control is a free form textbox. So if the user types in terms it searches instantly. Any further terms that are typed in continue to restrict the results. So you can type in Bob, or type in Bob Barker. Or type in Bob Barker Price is Right. The more criteria typed in the more narrowed your result. I bind the results to a gridview.
Moving forward what all do I need to do? From a high level, I assume I need to:
1) Go to Project Properties --> Advanced Compiler Settings and change the Target framework to 3.5 from 2.0.
2) Add the reference to System.XML.Linq, Add the Imports statement to the classes.
So I'm not sure what the best approach is going forward after that. I assume I use XDocument.Load, then my search subroutine runs against the XDocument. Do I just do the standard Linq query for this sort of repeated search? Like so:
Dim people =
from phonebook in doc.Root.Elements("phonebook")
where phonebook.Element("userid") = "whatever"
select phonebook
Any tips on how to best implement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该视频向您展示如何从 XML 文档启用智能感知。我广泛使用这个。希望它有帮助:
http://www.asp.net/learn/linq-videos/ video-216.aspx
然后您可以使用已选择的项目列表将其从结果集中排除。
This video shows you how to enable intellisense from XML documents. I use this extensively. Hopefully it helps:
http://www.asp.net/learn/linq-videos/video-216.aspx
Then you can use a list of items already selected to exclude those from the result set.
首先,我应该提到 var 用于 C# 而不是 VB.Net(使用 dim)。接下来,您可以根据需要多次重复使用针对 xml 的 linq 查询。除非有什么东西修改了内存中的 xml,否则应该没问题。
First, I should mention that var is used with C# and not VB.Net (use dim). Next, you can reuse you linq queries against the xml as often as you want. Unless something modifies the xml in memory, you should be ok.