views:

55

answers:

2

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?

+1  A: 

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.

Wade73
Thanks for noting that, I do realize that I just was looking over some articles on how to get up and running with Linq, and the code snippet was in C# and I quickly converted it by hand. I'll update the post.
Cj Anderson
+1  A: 

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.

Joseph Connolly
Thank you I upvoted you for the useful link. I'll check it out to see if it helps me feel comfortable moving forward. If so, I'll mark as answer.
Cj Anderson
There is a whole section on linq tutorials on the asp.net site. But since I am a newb I could only post one link per answer. You can search "linq tutorial" on the asp.net site. The videos were a great help.http://www.asp.net/learn/linq-videos/
Joseph Connolly