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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:43:08+00:00 2026-05-16T18:43:08+00:00

With an ASP.NET MVC project I’m working on, I am required to check whether

  • 0

With an ASP.NET MVC project I’m working on, I am required to check whether bit variables within a LINQ-To-SQL class are true. So far, After checking whether or not each variable is true or false, I then push the value of the field into a List and return it like so:

public List<String> GetVarList() {
    List<String> list = new List<String>();

    if (fields.SearchBar) {
        list.Add("SearchBar");
    }

    if (fields.SomeField) {
        list.Add("SomeField");
    }

    return list;
}

This, to me, doesn’t seem to be the fastest or easiest way to do it.

I was wondering its possible to somehow be able check the value of the variable dynamically from an array of strings by looping through them with either a for or a foreach loop. For instance:

public List<String> GetVarList() {
    String[] array = {"SearchBar", "SomeField"};
    List<String> list = new List<String>();

    foreach (String field in array) {
        // Check whether or not the value is true dynamically through the array
    }

    return list;
}

Thanks for any suggestions!

  • 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-16T18:43:08+00:00Added an answer on May 16, 2026 at 6:43 pm

    Certainly, you can use reflection for something like this:

    private bool ValueWasSet(string propertyName)
    {
        var property = fields.GetType().GetProperty(propertyName);
        return (bool)property.GetValue(fields, null);
    }
    
    public List<string> GetVarList()
    {
        return new [] {"SearchBar", "SomeField"}
            .Where(ValueWasSet)
            .ToList();
    }
    

    It is a very straight-forward solution to what you want to do, assuming you have a lot of items to look through.

    CAVEAT: This is NOT faster than your code. Your code is much faster than this… but if you want to do it more dynamically, you have to pay a slight perf price.

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

Sidebar

Related Questions

I am working on an ASP.NET MVC project which allows users to construct arbitrarily
I'm working on my first real ASP.NET MVC project and I've noticed that the
I am working on a ASP .NET mVC project & i have to change
I am working with an ASP.NET MVC project which was originally started from the
My ASP.NET MVC project has some chapter class and related tag class They have
I have an ASP.NET MVC project containing an AdminController class and giving me URls
I have an asp.net mvc project and reference a dll to a class library.
I'am working on small ASP.NET MVC project and I got an weird behaviour. At
I've been working on a ASP.NET MVC project with LinqToSql. The application has 3
I am using an ASP.NET MVC project and everytime I add a class to

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.