Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 145937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:30:44+00:00 2026-05-11T08:30:44+00:00

I have a course table which I need to search based on keywords typed

  • 0

I have a course table which I need to search based on keywords typed in the search box. Here is a sample query:

SELECT * FROM Courses WHERE  Title LIKE '%word%' OR Title LIKE '%excel%' OR  Contents LIKE '%word%' OR Contents LIKE '%excel%' 

How can I convert this in LINQ where LINQ would dynamically generate WHERE statements based on each keywords.

I tried to user PredicateBuilder it works fine as long as the field is VARCHAR. For the ‘TEXT’ fields the quotes are not generated thus causing compiler to give an error message. Here is the SQL generated by PredicateBuilder

SELECT [t0].[CoursesID], [t0].[Title], [t0].[Contents], [t0].[Active],  FROM [dbo].[Courses] AS [t0] WHERE ([t0].[Title] LIKE '%word%') OR ([t0].[Contents] LIKE %word%) OR  ([t0].Title] LIKE '%excel%') OR ([t0].[Contents] LIKE %excel%) 

Notice there is no single Quote for the ‘Contents’ field which is a Text field in the database.

Is there any easy way to build WHERE statement and attach it with query? Does anyone know how I can do this without PredicateBuilder?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:30:45+00:00Added an answer on May 11, 2026 at 8:30 am

    Since you are working w/ LINQ I suppose you are working against a LINQ-to-SQL data context right? I don’t have a spare DataContext lying around to test this, but this should give you some ideas.

    I don’t know if it will work against data context though, but most of these are pretty basic stuff (chaining OR operator and Contains method call) so it shouldn’t cause problem when the query translates to SQL.

    First I create a custom function that would build my predicate:

    Func<string, Func<DataItem, bool>> buildKeywordPredicate =     keyword =>         x => x.Title.Contains(keyword)             || x.Contents.Contains(keyword); 

    This is a function which takes a single string keyword and then return another function which takes a DataItem and checks it against the keyword.

    Basically, if you pass in ‘Stack’, you’ll get a predicate: x => x.Title.Contains('Stack') || x.Contents.Contains('Stack').

    Next, since there are many possible keywords and you need to chain it with an OR operation, I create another helper function to chain 2 predicates together with an OR

    Func<Func<DataItem,bool>, Func<DataItem, bool>, Func<DataItem, bool>> buildOrPredicate =     (pred1, pred2) =>         x => pred1(x) || pred2(x); 

    This function takes 2 predicates and then join them up with an OR operation.

    Having those 2 functions, I can then build my where predicate like this:

    foreach (var word in keywords) {                 filter = filter == null         ? buildKeywordPredicate(word)         : buildOrPredicate(filter, buildKeywordPredicate(word)); } 

    The first line inside the loop basically checks if the filter is null. If it is, then we want a simple keyword filter built for us.

    Else if the filter is not null, we need to chain existing filters with an OR operation, so we pass the existing filter and a new keyword filter to buildOrPredicate to do just that.

    And then we can now create the WHERE part of the query:

    var result = data.Where(filter); 

    Passing in the complicated predicate we’ve just built.

    I don’t know if this will different from using PredicateBuilder but since we are deferring query translation to the LINQ-to-SQL engine, there should not be any problems.

    But as I said, I havn’t tested it against a real data context, so if there’s any problems you can write in the comments.

    Here’s the console app that I built to test: http://pastebin.com/feb8cc1e

    Hope this helps!


    EDIT: For a more generic and reusable version which involves properly utilizing the Expression Trees in LINQ, check out Thomas Petricek’s blog post: http://tomasp.net/articles/dynamic-linq-queries.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 253k
  • Answers 253k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can download 30 days vtune trial and try by… May 13, 2026 at 10:01 am
  • Editorial Team
    Editorial Team added an answer Depends on your datastore, and if using DataNucleus. There is… May 13, 2026 at 10:01 am
  • Editorial Team
    Editorial Team added an answer I just found this Oracle example on Wikipedia that looks… May 13, 2026 at 10:01 am

Related Questions

I have the following problem. I have brain regions and correlations between them. The
I'm building a heavily CRUD based ASP.NET website and I've got to the phase
I have a bit of a puzzle (at least for me) which I am
I have a social networking site where users update their moods, profiles and add

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.