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 8401759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:53:15+00:00 2026-06-09T21:53:15+00:00

I am trying to query a database using LINQ. I am joining TableA with

  • 0

I am trying to query a database using LINQ. I am joining TableA with TableB with TableC.

I have zero to many ‘keywords’ (don’t know how many at design time) that I would like to look for within (LIKE ‘%%’) several fields that are spread across the three tables.

Assuming three (3) keywords are entered into my search box:

In T-SQL I would have this –

SELECT tbl0.FieldA, tbl0.FieldB, tbl1.FieldC, tbl1.FieldD, tbl2.FieldE, tbl2.FieldF

FROM tbl0

JOIN tbl1 ON tbl0.KeyField = tbl1.KeyField

JOIN tbl2 ON tbl1.KeyField = tbl2.KeyField

WHERE (tbl0.FieldA LIKE '%{keyword1}%' OR tbl1.FieldC LIKE '%{keyword1}%' OR tbl2.FieldE LIKE '%{keyword1}%' OR tbl0.FieldA LIKE '%{keyword2}%' OR tbl1.FieldC LIKE '%{keyword2}%' OR tbl2.FieldE LIKE '%{keyword2}%' OR tbl0.FieldA LIKE '%{keyword3}%' OR tbl1.FieldC LIKE '%{keyword3}%' OR tbl2.FieldE LIKE '%{keyword3}%')

Question is — How do I ‘dynamically’ build this WHERE clause in LINQ?

NOTE #1 — I do not (for reasons outside the scope of this question) want to create a VIEW across the three tables

NOTE #2 — Because I am joining in this way (and I am still new to LINQ) I don’t see how I can use the PredicateBuilder because I am not sure what TYPE (T) to pass into it?

NOTE #3 — If it matters … I am ultimately planning to return a strongly typed list of (custom) objects to be displayed in a GridView.

EDIT – 8/17/2012 – 5:15 PM EDT

The comment below is correct.

“The code the OP is looking for is where any one of the fields contains any one of the keywords.”

Thanks everyone!

  • 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. Editorial Team
    Editorial Team
    2026-06-09T21:53:16+00:00Added an answer on June 9, 2026 at 9:53 pm

    Here’s a solution not using the PredicateBuilder. Just get all the items containing the first keyword and merge it with all the items containing the second keyword and so on. Not knowing anything about the context of the problem I can’t tell if this will be efficient or not.

    var query = from t0 in db.Table0
                join t1 in db.Table1 on t0.KeyField equals t1.KeyField
                join t2 in db.Table2 on t1.KeyField equals t2.KeyField
                select new
                {
                    t0.FieldA, t0.FieldB,
                    t1.FieldC, t1.FieldD,
                    t2.FieldE, t2.FieldF
                };
    
    string keyword = keywordsList[0];
    var result = query.Where(x => x.FieldA.Contains(keyword) ||
                                  x.FieldC.Contains(keyword) ||
                                  x.FieldE.Contains(keyword));
    
    for (int i = 1; i < keywordsList.Length; i++)
    {
        string tempkey = keywordsList[i];
        result = result.Union(query.Where(x => x.FieldA.Contains(tempkey) ||
                                               x.FieldC.Contains(tempkey) ||
                                               x.FieldE.Contains(tempkey)));
    }
    
    result = result.Distinct();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to query a MySQL database using an array but I'm having trouble!
I am trying to query some tables in my database using a simple dropdown
I am trying to return voice messages from a database using this query :
I am trying to query SQL Server database from C# I have class Class_A
Im trying to create a select command to query the database I have and
I have a database on an external server that I am trying to query.
I am returning two lists from the database using LINQ to SQL compiled query.
I'm trying to write a linq-to-sql query using || that behaves the same way
I have the following LINQ query that returns two objects from my database. These
I am trying to run a linq query using EF on .NET 3.5 and

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.