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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:26:23+00:00 2026-05-28T15:26:23+00:00

Anyone want to help make me a better programmer? I have an application where

  • 0

Anyone want to help make me a better programmer?

I have an application where I am filtering a collection of objects on a variety of criteria. Right now my code works, but 90% is duplicated which I feel like means I am doing something wrong. Any suggestions on how to make more reusable filtering code?

Assuming I have a collection of objects like this:

    public class ExampleData
{
    public int SomeValue1 { get; set; }
    public int SomeValue2 { get; set; }
    public int SomeValue3 { get; set; }
    public string SomeValue4 { get; set; }
}

And here is my filter class. Look at the code comments.

  class ExampleFilter
{
    public ExampleFilter()
    {
    }

    public IEnumerable<ExampleData> applyFilters(SearchCriteria criteria, IEnumerable<ExampleData> data)
    {
        //The body of these methods is almost identical...how can I better design this process so I don't cut and paste 90% of the code?
        data = filterByValue1Selections(criteria.FormTemplateSelected, data);
        data = filterByValue2Selections(criteria.FormTemplateSelected, data);
        return data;
    }

    public IEnumerable<ExampleData> filterByValue1Selections(List<int> value1Ids, IEnumerable<ExampleData> data)
    {
        if (value1Ids != null)
        {
            IEnumerable<ExampleData> dataQuery = null;
            foreach (int selectedValueIds in value1Ids)
            {
                //See http://justgeeks.blogspot.com/2011/01/using-linq-in-foreach-loop-to-build.html
                //For explanation of why we copy this locally
                int selectedId = selectedValueIds;
                if (dataQuery == null)
                {
                    //This code and the similar block in the else are the only differences in these methods
                    dataQuery = data.Where(t => t.SomeValue1 == selectedId);
                }
                else
                {
                    dataQuery = dataQuery.Union(data.Where(t => t.SomeValue1 == selectedId));
                }
            }
            data = dataQuery;
        }
        return data;
    }

    public IEnumerable<ExampleData> filterByValue2Selections(List<int> value2Ids, IEnumerable<ExampleData> data)
    {
        if (value2Ids != null)
        {
            IEnumerable<ExampleData> dataQuery = null;
            foreach (int selectedValueIds in value2Ids)
            {
                //See http://justgeeks.blogspot.com/2011/01/using-linq-in-foreach-loop-to-build.html
                //For explanation of why we copy this locally
                int selectedId = selectedValueIds;
                if (dataQuery == null)
                {
                    dataQuery = data.Where(t => t.SomeValue1 == selectedId);
                }
                else
                {
                    dataQuery = dataQuery.Union(data.Where(t => t.SomeValue1 == selectedId));
                }
            }
            data = dataQuery;
        }
        return data;
    }


}
  • 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-28T15:26:24+00:00Added an answer on May 28, 2026 at 3:26 pm

    You are basically re-implementing the Contains() method – you can just do:

    List<int> valueIds = ...;
    var filteredData = data.Where(x=> valueIds.Contains(x.SomeValue1));
    

    Then your applyFilters method would look like this:

    public IEnumerable<ExampleData> applyFilters(SearchCriteria criteria, IEnumerable<ExampleData> data)
    {
        data = data.Where(x=> criteria.FormTemplateSelected.Contains(x.SomeValue1)
                           && criteria.FormTemplateSelected.Contains(x.SomeValue2));
        return data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help me out please? I'm confused. I want to set up my
I want to Integrate Weather radar on my MapView.Please anyone help on doing this
Hi i want to load png images and jpeg images. can anyone help me?
I want to develop android based application in c#.Can anyone tell me where can
I want to make an application that draws a layout with some background and
I want to make application in symbian OS mobile which can work in all
All I want is to make a simple user password login. I have put
Hello everyone i need some help. I'm trying to make an application for mac
I need to make a web application and I want to use MVC. However,
can anyone help, we have a sql server 2005 database installed (actually its an

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.