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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:33:52+00:00 2026-06-17T09:33:52+00:00

I have a piece of code like below: var selected = A; bool isSelected

  • 0

I have a piece of code like below:

var selected = “A”;
bool isSelected = selected == "A" || selected == "C";
var codeLists = new
{
    displayProperty1 = isSelected ? "property1" : null,
    displayProperty2 = isSelected ? "property2" : null,
    displayProperty3 = selected == "C" ? "property3" : null
};

So, my goal is to eliminate a property if it does not satisfy a condition. In the above code, selected is "A". So, displayProperty3 would have a value of null. But I want to eliminate displayProperty3 so that if selected is “A”, then there should be only 2 properties in the object.

If there is any proper and efficient way to do this, I would be grateful for it.

  • 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-17T09:33:52+00:00Added an answer on June 17, 2026 at 9:33 am

    No, an anonymous type still follows the rules of other types, they’re just not explicitly defined at compile-time. To do what you want you’d have to define two different types.

    If you don’t want to show that property in your UI (e.g. if you ware binding to a grid that’s auto-generated and you don’t want that to be a column) then deal with that in your UI.

    However, if you HAVE to do this, you’d have to create two different types (either anonymous or explicit):

    var selected = "A";
    bool isSelected = selected == "A" || selected == "C";
    dynamic codeLists;
    if(selected == "C")
    {
        codeLists = new
        {
            displayProperty1 = isSelected ? "property1" : null,
            displayProperty2 = isSelected ? "property2" : null
        }; 
    }
    else
    {
        codeLists = new
        {
            displayProperty1 = isSelected ? "property1" : null,
            displayProperty2 = isSelected ? "property2" : null,
            displayProperty3 = "property3" 
        }; 
    }
    

    It would be better if you created a base type with the common properties, but either way they are going to be two different types:

    public class CodeList
    {
        public string displayProperty1 {get; set;}
        public string displayProperty2 {get; set;}
    }
    
    public class CodeListC : CodeList
    {
        public string displayProperty3 {get; set;}
        // Other two properties will be inherited
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have piece of code like below: var workStream = new MemoryStream(); var doc =
Have piece of code like below: var workStream = new MemoryStream(); var doc =
I have a piece of code like this: HashSet<Object> Set = new HashSet<Object>(); //add
I have a piece of code like so... FileInputStream fi = new FileInputStream(filein); GZIPInputStream
I have a piece of shell code run.sh like below: #!/bin/sh PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH export LD_LIBRARY_PATH=../../lib
I have a piece of code that looks something like below and it works
I have a piece of code something like below. I'm using Microsoft Visual C#
I have seen a piece of sql injection example code like below, what does
I have a piece of code that in principal looks like the below. The
I have a piece of MVC 2.0 html code is like below, I want

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.