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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:40:08+00:00 2026-05-26T07:40:08+00:00

How do you write a dynamic Linq query for the following simple search criteria?

  • 0

How do you write a dynamic Linq query for the following simple search criteria?
1) StudentNumber
2) LastName
3) LastName and FirstName

//if (!String.IsNullOrEmpty(StudentNumber))
var results  = (from s in Students              
                where s.StudentNumber == 1001
                select s
               );

//else if (!String.IsNullOrEmpty(LastName) & (String.IsNullOrEmpty(FirstName))

var results  = (from s in Students              
                where s.LastName == "Tucker"
                select s
               );

//else if (!String.IsNullOrEmpty(LastName) & (!String.IsNullOrEmpty(FirstName))            
var results  = (from s in Students              
                where s.LastName == "Tucker" && s.FirstName == "Ron"
                select s
               );   
  • 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-26T07:40:08+00:00Added an answer on May 26, 2026 at 7:40 am

    You need to declare your results variable outside of any individual query. This will allow you append different filters based upon your varying criteria, and append as many filters as you need. An example:

    var results = Students.AsEnumerable(); // use .AsQueryable() for EF or Linq-to-SQL
    
    if (!string.IsNullorEmpty(StudentNumber)) 
    {
        results = results.Where(s => s.StudentNumber.Equals(StudentNumber));
    }
    else if (!string.IsNullOrEmpty(LastName))
    {
        results = results.Where(s => s.LastName.Equals(LastName));
    
        if (!string.IsNullOrEmpty(FirstName))
        {
             results = results.Where(s => s.FirstName.Equals(FirstName));
             // filter is in addition to predicate against LastName
        }
    }
    
    // results can be used here
    

    If dealing with Linq-to-Entities or -Sql, type the initial query with Students.AsQueryable(); so that the filtering happens at the database rather than inside the application.


    Is there a way I can construct the WHERE clause first and use it in a
    Linq query without if…else

    If you want to build the entire where before the first step of the query, it’s the same logic. You are conditionally building the predicate, so you will have some sort of if/else involved. However, to build the entire predicate first, you could build against a Func<Student, bool> for Linq to Objects.

    Func<Student, bool> predicate;
    if (!string.IsNullOrEmpty(StudentNumber))
    {
        predicate = s => s.StudentNumber.Equals(StudentNumber);
    }
    else if (!string.IsNullOrEmpty(LastName))
    {
        predicate = s => s.LastName.Equals(LastName);
    
        if (!string.IsNullOrEmpty(FirstName))
        {
            Func<Student, bool> p = predicate;
            predicate = s => p(s) && s.FirstName.Equals(FirstName);
        }
    }
    else
    {
        predicate = s => true;
    }
    
    var query = Students.Where(predicate);
    

    You’ll notice it’s the exact same if/else structure. You could collapse that down into a complicated conditional expression

    Func<Student, bool> predicate;
    predicate = s =>
        !string.IsNullOrEmpty(StudentNumber)
        ? s.StudentNumber.Equals(StudentNumber)
        : !string.IsNullOrEmpty(LastName)
            ? !string.IsNullOrEmpty(FirstName)
                ? s.LastName.Equals(LastName) && s.FirstName.Equals(FirstName)
                : s.LastName.Equals(LastName)
            : true;
    
    var query = Students.Where(predicate);
    

    But I find that pretty well difficult to follow, certainly as compared to the longer if/else. This predicate is also bigger than the one we build via the if/else, because this one contains all the logic, it’s not just the logic we conditionally added.

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

Sidebar

Related Questions

I'm trying to write a dynamic Linq 2 Sql query using Expressions trees but
how can i write dynamic javascript any simple example or any references. thanking you
I would like to write a hql query using a dynamic instantiation with a
I am trying to write a Dynamic Query which uses a CTE. But I
Hi I am looking for best method for writing Dynamic LINQ query. I have
When Executing the following linq to sql statement: var stuff = from l in
I am trying to write a dynamic search I and chain where clauses I
I've build a large program with many references. F.e.: System.Data.DataSetExtensions System.Linq.Dynamic I've to write
Basically I want to write a linq query to order the number of days
What is the best way to write a dynamic parametrized query for sql server

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.