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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:29:49+00:00 2026-05-13T23:29:49+00:00

I was just reading a recent question on using conditionals in Linq and it

  • 0

I was just reading a recent question on using conditionals in Linq and it reminded me of an issue I have not been able to resolve. When building Linq to SQL queries programmatically how can this be done when the number of conditionals is not known until runtime?

For instance in the code below the first clause creates an IQueryable that, if executed, would select all the tasks (called issues) in the database, the 2nd clause will refine that to just issues assigned to one department if one has been selected in a combobox (Which has it’s selected item bound to the departmentToShow property).

How could I do this using the selectedItems collection instead?

IQueryable<Issue> issuesQuery;

// Will select all tasks
issuesQuery = from i in db.Issues
              orderby i.IssDueDate, i.IssUrgency
              select i;

// Filters out all other Departments if one is selected
   if (departmentToShow != "All")
   {
        issuesQuery = from i in issuesQuery
                      where i.IssDepartment == departmentToShow
                      select i;
    }

By the way, the above code is simplified, in the actual code there are about a dozen clauses that refine the query based on the users search and filter settings.

  • 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-13T23:29:50+00:00Added an answer on May 13, 2026 at 11:29 pm

    If the number of conditions is unknown then it’s easier to use lambda syntax instead of query comprehension, i.e.:

    IQueryable<Issue> issues = db.Issues;
    if (departmentToShow != "All")
    {
        issues = issues.Where(i => i.IssDepartment == departmentToShow);
    }
    issues = issues.OrderBy(i => i.IssDueDate).ThenBy(i => i.IssUrgency);
    

    (Assuming you want the ordering to happen after the filtering, which ought to be the case – I’m not sure if Linq will generate an optimized query if you try to do the ordering first).

    If you’ve got a very large number of optional conditions then you can clean it up with predicates:

    List<Predicate<Issue>> conditions = new List<Predicate<Issue>>();
    if (departmentToShow != "All")
        conditions.Add(i => i.IssDepartment == departmentToShow);
    if (someOtherThing)
        conditions.Add(anotherPredicate);
    // etc. snip adding conditions
    
    var issues = from i in issues
                 where conditions.All(c => c(i))
                 orderby i.IssDueDate, i.IssUrgency;
    

    Or just use PredicateBuilder which is probably easier.

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

Sidebar

Related Questions

Just curious. Using gcc/gdb under Ubuntu 9.10. Reading a C book that also often
just reading up on spring, and when using DI in spring, if you set
This is my first post here. I have been reading posts here since I
In a recent question I was encouraged to try using some basic data structures
My recent turn-on is using BinaryFormatter instead of some database product. I have an
Just reading the DevIL manual here . Also reminded me of glGenBuffers . Why
Was just reading about different algos disassemblers use to identify binary as assembly instructions.
i was just reading a great presentation about Quality Assurance for PHP Projects by
I'm just reading about OutputCache , and I see how you can apply VaryByParam
I was just reading abit about JMS and Apache ActiveMQ. And was wondering what

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.