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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:25:20+00:00 2026-05-10T15:25:20+00:00

Not sure if this is possible or if I’m expressing correctly what I’m looking

  • 0

Not sure if this is possible or if I’m expressing correctly what I’m looking for, but I have the following piece of code in my library repeatedly and would like to practice some DRY. I have set of SQL Server tables that I’m querying based on a simple user-supplied search field ala Google. I’m using LINQ to compose the final query based on what’s in the search string. I’m looking for a way to use generics and passed in lambda functions to create a reusable routine out of this:

string[] arrayOfQueryTerms = getsTheArray();  var somequery = from q in dataContext.MyTable                 select q;  if (arrayOfQueryTerms.Length == 1) {     somequery = somequery.Where<MyTableEntity>(         e => e.FieldName.StartsWith(arrayOfQueryTerms[0])); } else {     foreach(string queryTerm in arrayOfQueryTerms)     {         if (!String.IsNullOrEmpty(queryTerm))         {             somequery = somequery                          .Where<MyTableEntity>(                             e => e.FieldName.Contains(queryTerm));         }     } } 

I was hoping to create a generic method with signature that looks something like:

private IQueryable<T> getQuery(     T MyTableEntity, string[] arrayOfQueryTerms, Func<T, bool> predicate) 

I’m using the same search strategy across all my tables, so the only thing that really differs from usage to usage is the MyTable & MyTableEntity searched and the FieldName searched. Does this make sense? Is there a way with LINQ to dynamically pass in the name of the field to query in the where clause? Or can I pass in this as a predicate lambda?

e => e.FieldName.Contains(queryTerm) 

I realize there a million and a half ways to do this in SQL, probably easier, but I’d love to keep everything in the LINQ family for this one. Also, I feel that generics should be handy for a problem like this. Any ideas?

  • 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-10T15:25:20+00:00Added an answer on May 10, 2026 at 3:25 pm

    It sounds like you’re looking for Dynamic Linq. Take a look here. This allows you to pass strings as arguments to the query methods, like:

    var query = dataSource.Where('CategoryID == 2 && UnitPrice > 3')                       .OrderBy('SupplierID'); 

    Edit: Another set of posts on this subject, using C# 4’s Dynamic support: Part 1 and Part 2.

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

Sidebar

Related Questions

Not sure this is possible, but looking to write a script that would return
I'm not sure if this is possible (complete non-flash developer speaking), but we have
I'm not sure if this is possible but as I was looking for a
Not entirely sure this is possible, but hoping to be pleasantly surprised. I have
Not sure if this is possible, but I have a field in a table
I'm not sure if this is even possible but here goes: Currently I have
I'm not sure this is possible, but the code I tried using first is:
I'm not sure this is even possible, but here goes: I have a class
Not sure this is possible but maybe you can help. I have a list,
I'm not sure this is possible, but in ruby, you can dynamically call a

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.