I would like to search my database for products from the table (products). The problem is that I do not want 600 lines of code with multiple ifs. The code is as follows: (I don't want this to be so)
Public Function GetSearchResults(ByVal County As Integer, Optional ByVal Searchtext As String = "", Optional ByVal Category As Integer = 0, Optional ByVal SubCategory As Integer = 0) As List(Of Product)
Dim dc As New DataClassesDataContext
Dim Listholder As New List(Of Product)
If Searchtext IsNot "" Then
If County > 0 Then
If Category > 0 Then
If SubCategory = 0 Then
Dim Results = From p In dc.Products _
Where p.Headline.Contains(Searchtext) _
Where p.CategoryID = Category _
Where p.CountyID = County _
Select p
Listholder = Results.ToList
Return Listholder.ToList
And a lot of elseifs etc. The problem is that if the value is 0 for any of the above, the search will be for all countys / categorys / headlines .... is there a better way to do this? I mean that linq is great, there should be a way to make it more dynamic, so I don't need IFS.
source
share