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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:38:27+00:00 2026-05-25T00:38:27+00:00

I have a search form that looks like this: The code behind the form

  • 0

I have a search form that looks like this:

search form

The code behind the form looks like this:

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <div>
        @Html.DropDownList("SelectedType", Model.TypeOptions)
        @Html.DropDownList("SelectedSearch", Model.SearchOptions)
        @Html.TextBoxFor(x => x.SearchTerm)
        <input type="submit" value="Search" />
    </div>
}

What I want to do is dynamically construct a lambda where clause from the return options. E.g. if the user selects “Process No” and “Contains” then the lambda would look like

model.DataSource = _db.InstrumentLists.Where(x => x.Process_No.Contains(SearchTerm));

Or if the user selects “PLC No” and “Equals” then the lambda would look like

model.DataSource = _db.InstrumentLists.Where(x => x.PLC_No == SearchTerm);

I am trying to do this while avoiding a big case statement or an if stack, i.e. I don’t want the following:

if (SelectedType == "Process No" And SelectedSearch = "Contains")
   model.DataSource = _db.InstrumentLists.Where(x => x.Process_No.Contains(SearchTerm));
elseif (SelectedType == "Process No" And SelectedSearch = "Equals")
   model.DataSource = _db.InstrumentLists.Where(x => x.Process_No == SearchTerm);
...

Essentially I want to pass a reference to a class property, something to specify the test type (i.e contains, equals, starts with, etc) and a search term to a function, or something along those lines, and get back a predicate to put into my Where clause. I want this function to work dynamically so I shouldn’t have to modify it for every combination of property and test type.

Is this possible or is the only way to use Where with a string predicate parameter?

Edit: In case its important, I’m use EF for my data model so _db.InstrumentLists returns an ObjectSet<InstrumentList>.

  • 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-05-25T00:38:27+00:00Added an answer on May 25, 2026 at 12:38 am

    you have 2 options here:

    • make a “switch” based search method i.e. depending on the value of user selection executes a different Where and returns a DataSource

    • use Dynamic Linq and construct the Where from a string

    EDIT – example for Dynamic Linq:

    model.DataSource = _db.InstrumentLists.Where(SelectedType + " == @0", SearchTerm);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage that looks like: <html> <head> <title>Hi</title> </head> <body> <form name=mainForm
(Using MySQL and PHP) I have a search form that will allow my users
I have an ASP.NET page that is basically a search form. My code for
I have a data structure that looks like this: public class Node { public
I have a class that looks like this: public class UserListVM { public SearchModel
I have a form that submits a search for blogs on my site via
The functionality I'm looking for: I have a form that will search my 'Proposal'
Let us say that I have a form to search by multiple tags in
I have a stored procedure that uses this select statement: SELECT dbo.VendorProgram.id, dbo.VendorProgram.CODE, dbo.Programs.ProgramName
I'm using PHP for my server side language and have a form that returns

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.