jvcoach23 Posted May 6, 2008 Posted May 6, 2008 I have a custom class QBank with a public property called QBankID. I populate the ilist(to QBank). when the iList is populated, is there a way i can filter on the QBankID property for where it = x. I could filter this in the database call.. but i'd like to bring back a larger dataset and only make one call to the database for the data retrieval, then in code filter for the id i want, work on that.. and than filter for another id and so on. i'm just trying to limit my db calls. I could loop through the iList to get the value, just wondering if there is a better way.. thanks shannon Quote JvCoach23 VB.Net newbie MS Sql Vet
Nate Bross Posted May 6, 2008 Posted May 6, 2008 Unless there are extenuating circumstances, the data server is the best place to filter this data. Unless the data server is slow (xml via http for example) multiple calls to a db should be OK. What type of database are you using? SQL Server should be able to handle whatever you throw at it; even MS Access can handle multiple queries in a short period of time. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
jvcoach23 Posted May 6, 2008 Author Posted May 6, 2008 It's Sql server.. i was going to have about 80 queries that would have to hit the sql box. i was just looking to optimize things.. if i can do just one query, then filter in code, that would be faster than doing the 80 queries. is filtering a class on a property hard to do??? Quote JvCoach23 VB.Net newbie MS Sql Vet
Administrators PlausiblyDamp Posted May 6, 2008 Administrators Posted May 6, 2008 You could potentially use a dataview - that may save repeated database calls. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jvcoach23 Posted May 7, 2008 Author Posted May 7, 2008 well.. right now i'm not doing any interface work, this is all just in the background.. logic that will eventually write data to the database. i'm doing it in code because there is some string munipulation that would be much harder for me to do in sql.. So i'm looking for ways to filter the data in the class thanks Quote JvCoach23 VB.Net newbie MS Sql Vet
Administrators PlausiblyDamp Posted May 7, 2008 Administrators Posted May 7, 2008 Depends on the version you are using for .Net you could do something like Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim customers As New List(Of Customer) customers.Add(New Customer(1, "test")) customers.Add(New Customer(2, "Another test")) customers.Add(New Customer(3, "test again")) customers.Add(New Customer(4, "testing")) customers.Add(New Customer(5, "final test")) Dim results As List(Of Customer) = customers.FindAll(AddressOf BeginsWithT) For Each c1 As Customer In results Debug.WriteLine(c1.Name) Next End Sub Private Shared Function BeginsWithT(ByVal c As Customer) As Boolean Return c.Name.StartsWith("t") End Function End Class Public Class Customer Public Sub New(ByVal id As Integer, ByVal name As String) _Id = id _Name = name End Sub Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Private _Id As Integer Public Property NewProperty() As Integer Get Return _Id End Get Set(ByVal value As Integer) _Id = value End Set End Property End Class The above (contrived) example fills a list with a custom class and then uses a predicate filter (The beginswitht method) to return a filtered list of results. If you are using .Net 3 or later then Linq could also be used Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim customers As New List(Of Customer) customers.Add(New Customer(1, "test")) customers.Add(New Customer(2, "Another test")) customers.Add(New Customer(3, "test again")) customers.Add(New Customer(4, "testing")) customers.Add(New Customer(5, "final test")) ' Dim results As List(Of Customer) = customers.FindAll(AddressOf BeginsWithT) Dim results = From c In customers Where c.Name.StartsWith("t") Select c For Each c1 As Customer In results Debug.WriteLine(c1.Name) Next End Sub End Class Public Class Customer Public Sub New(ByVal id As Integer, ByVal name As String) _Id = id _Name = name End Sub Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Private _Id As Integer Public Property NewProperty() As Integer Get Return _Id End Get Set(ByVal value As Integer) _Id = value End Set End Property End Class Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jvcoach23 Posted May 7, 2008 Author Posted May 7, 2008 thank you, i'll give it a try Quote JvCoach23 VB.Net newbie MS Sql Vet
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.