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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
First link was spot on! the other two... eh.

thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.