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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:33:03+00:00 2026-06-18T04:33:03+00:00

I have a list with Article objects. Parameters for this object are Category, Description,

  • 0

I have a list with Article objects.
Parameters for this object are

Category, Description, Status, Class

The user can filter the list with the combinaison he wants. Only Description, or Category + Class, an so on.

So if the user choose a criterion i’ll get an int >= 0, otherwise i’ll get -1.

For example if he choose to filter with Status and Category, i’ll get

FilterCategory = x, FilterDescription = -1, FilterStatus = y, FilterClass = -1

My way to filter the list is the following:

if (FilterCategory != -1)
    list = list.Where(a => a.Category == FilterCategory);
if (FilterDescription != -1)
    list = list.Where(a => a.Description == FilterDescription);
if (FilterStatus != -1)
    list = list.Where(a => a.Status == FilterStatus);
if (FilterClass != -1)
    list = list.Where(a => a.Class == FilterClass);

In this way, i have to iterate the list 4 time, it’s not efficient with a lot of items. I would browse the list once, and check the 4 condition in an unique where. But i don’t know how to do that with the specific condition != -1.

Thank you

  • 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-06-18T04:33:04+00:00Added an answer on June 18, 2026 at 4:33 am

    You can do a HUGE Where query moving all your conditions it a single Where by noticing that:

    if (FilterCategory != -1)
        list = list.Where(a => a.Category == FilterCategory);
    

    is equivalent to:

    list = list.Where(a => FilterCategory == -1 || a.Category == FilterCategory);
    

    Then the query is:

    list = list.Where(a => (FilterCategory == -1 || a.Category == FilterCategory)
                        && (FilterDescription == -1 || a.Description == FilterDescription)
                        && (FilterStatus == -1 || a.Status == FilterStatus)
                        && (FilterClass == -1 || a.Class == FilterClass));
    

    But I really doubt it will improve your enumeration performance.

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

Sidebar

Related Questions

Let's say, we have and list of objects in variable called articles, each object
I have the following Django 1.2 models: class Category(models.Model): name = models.CharField(max_length=255) class Article(models.Model):
I have a list of articles, and each article has its own title and
Consider following scenario: I have RESTful URL /articles that returns list of articles user
i have list of rows that user select and i want to delete them,
So, I'm doing an assignment for my C# class and I have a list
I have a datagrid with a list of JobItem objects, and when I select
I have a List of objects that contain another List . I want to
I have a Tag and an Article object with a many-to-many relationship using Entity
Hi I want to do this in my views: recent_articles_list = Article.objects.order_by('-timestamp')[:3] headline_article =

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.