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

  • Home
  • SEARCH
  • 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 197701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:52:32+00:00 2026-05-11T16:52:32+00:00

Hi I am looking for best method for writing Dynamic LINQ query. I have

  • 0

Hi I am looking for best method for writing Dynamic LINQ query.

I have a function like

public IQueryable<Student> FindByAllStudents(int? id, string Name, int? CourseID, bool? IsActive) // like this way, all field values are passed
    {    
        // code for compairision
        return db.Student;
    }

we can also write db.Students.where(predicate)

or

a query like

var students = from s in db.students where s.Name.Contains(Name)
                s.ID.Equals(id)
                //and so on....

So will this method works if i don’t pass ID (i.e. Null)?
is proper way for all the datatypes?

The point is function can have all null values as a parameter for equivalence of select * from statement.

can any one help me to build best query with sample code?

  • 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-11T16:52:32+00:00Added an answer on May 11, 2026 at 4:52 pm

    Okay, it’s not entirely clear what you want, but if you’re trying to only add where clauses for the parameters which are non-null, you could do:

    public IQueryable<Student> FindByAllStudents
        (int? id, string name, int? courseID, bool? isActive)
    {    
        IQueryable<Student> query = db.Student;
        if (id != null)
        {
            query = query.Where(student => student.ID == id.Value);
        }
        if (name != null)
        {
            query = query.Where(student => student.Name.Contains(name));
        }
        if (courseID != null)
        {
            query = query.Where(student => student.CourseID == courseID.Value);
        }
        if (isActive != null)
        {
            query = query.Where(student => student.IsActive == isActive.Value);
        }
        return query;
    }
    

    I haven’t tried that, and it’s possible that LINQ to SQL would get confused by the code to find the value of the nullable value types. You may need to write code like this:

        if (courseID != null)
        {
            int queryCourseID = courseID.Value;
            query = query.Where(student => student.CourseID == queryCourseID);
        }
    

    It’s worth trying the simpler form first though 🙂

    Of course, all this gets a bit irritating. A helpful extension method could make life more concise:

    public static IQueryable<TSource> OptionalWhere<TSource, TParameter>
        (IQueryable<TSource> source,
         TParameter? parameter, 
         Func<TParameter, Expression<Func<TSource,bool>>> whereClause)
        where TParameter : struct
    {
        IQueryable<TSource> ret = source;
        if (parameter != null)
        {
            ret = ret.Where(whereClause(parameter.Value));
        }
        return ret;
    }
    

    You’d then use it like this:

    public IQueryable<Student> FindByAllStudents
        (int? id, string name, int? courseID, bool? isActive)
    {    
        IQueryable<Student> query = db.Student
            .OptionalWhere(id, x => (student => student.ID == x))
            .OptionalWhere(courseID, x => (student => student.CourseID == x))
            .OptionalWhere(isActive, x => (student => student.IsActive == x));
        if (name != null)
        {
            query = query.Where(student => student.Name.Contains(name));
        }
        return query;
    }
    

    Using a higher order function like this could get confusing if you’re not really comfortable with it though, so if you’re not doing very many queries like this you might want to stick with the longer but simpler code.

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

Sidebar

Related Questions

I am looking for the best method to run a Java Application as a
I'm looking for the best method to parse various XML documents using a Java
Im using Qt framework , and i looking for the best method to show
I'm curious what the best method of web 2.0 form validation is... I'm looking
So I have two custom complex types like this (oversimplified for this example): public
I am looking for best practices for detecting and preventing DOS in the service
I'm looking for best practices for establishing connections between Oracle 8 and Visual Studio
I'm looking for best practices for performing strict (whitelist) validation/filtering of user-submitted HTML. Main
I'm looking for best practices for using the same data in different places without
I am looking for best practices in regards to printing from a WinForms application.

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.