Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Starr Duskk

ASKER

First link was spot on! the other two... eh.

thanks!