Dynamically building LINQ based on search results

I have a search form, and depending on what selections were made in the form, I want to build my LINQ query. I do this with SQL, but don't know how to do it with LINQ.

I'm attaching an example of how I build the query string now. Could you please show me an example of how I might go about building a LINQ query the same way?

thanks.

Public Function GetBySearchCriteriaQuery(Optional ByVal CMSContentId As String = "", _
                                        Optional ByVal CMSTopicCode As String = "", _
                                          Optional ByVal CMSStatusCode As Integer = INVALID_ID) As String

            Dim sql As String = String.Empty
            Try

                Dim whereClause As New StringBuilder()

                If CMSContentId <> String.Empty And CMSContentId <> INVALID_ID.ToString Then
                    If whereClause.ToString() = String.Empty Then
                        whereClause.AppendLine(" WHERE ")
                    Else
                        whereClause.AppendLine(" AND ")
                    End If
                    whereClause.AppendLine(String.Format(" CMSContentId IN ({0}) ", CMSContentId))
                End If


                If CMSTopicCode <> String.Empty And CMSTopicCode <> INVALID_ID.ToString Then
                    If whereClause.ToString() = String.Empty Then
                        whereClause.AppendLine(" WHERE ")
                    Else
                        whereClause.AppendLine(" AND ")
                    End If
                    whereClause.AppendLine(String.Format(" CMSContent.CMSContentId IN (Select CMSContentID from CMSTopic where CMSTopicCode IN ({0})) ", CMSTopicCode))
                End If


                If CMSStatusCode <> INVALID_ID Then
                    If whereClause.ToString() = String.Empty Then
                        whereClause.AppendLine(" WHERE ")
                    Else
                        whereClause.AppendLine(" AND ")
                    End If
                    whereClause.AppendLine(String.Format(" CMSStatusCode = {0} ", CMSStatusCode))
                End If

                sql = String.Format("SELECT CMSContent.*, CatCode.ListValueName FROM {0} " & _
                               " LEFT OUTER JOIN ListValue AS CatCode ON CatCode.ListValueId = CMSContent.CMSCategoryCode " & _
                                " {1} " & _
                                " Order By {2} ", Me.DatabaseTableName, whereClause.ToString(), Me.OrderBy)

            Catch ex As Exception

            End Try

            Return sql

        End Function

Open in new window

LVL 2
Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
First link was spot on! the other two... eh.

thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.