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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:51:24+00:00 2026-05-27T18:51:24+00:00

I have an object model like this: public class MyClass { int TheProp {get;set;}

  • 0

I have an object model like this:

public class MyClass
{
  int TheProp {get;set;}
  List<Object1> TheListOfObject1 {get;set;}
  List<Object2> TheListOfObject2 {get;set;}
  List<Object3> TheListOfObject3 {get;set;}  
  List<Object4> TheListOfObject4 {get;set;}
}

Now some the the nested objects contain a property called MyField and some of the lists might have objects in them and some don’t.

I’m looking to loop through every list and if the objects in the list contain the property MyField then set that property to 1.

What’s the best way to do this? I could hard-code the loop and only loop through the lists for which I know there’s the property I’m looking for and then test if the list has a count > 0 but if I then add another list of nested object I’d have to rewrite that part too. What’s a more general way to do it?

Thanks.

  • 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-27T18:51:25+00:00Added an answer on May 27, 2026 at 6:51 pm

    Either use reflection or write an interface containing that property

    public interface IProperty {
        int Prop { get; set; }
    }
    

    and implement it in all your classes which have that property. Then you can use

    foreach (var obj in myList.OfType<IProperty>()) {
        obj.Prop = 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a object model a bit like this: public class Foo { public
I have an object model like this: class Car { public string Manufacturer; public
I have an object model like this: public class Quantity { public decimal Weight
I have an object model that looks like this: public class MyObjectModel { public
I have a model like this: public class MyModel { [ScaffoldColumn(false)] public int CharityId
Given I have a Model object like ... public class MyModel { public int
I have an object model somewhat like this: public class MyObject { public string
I have an object that looks like this: class Model { public string Category
I have a model (simplified for relevance) like this: public abstract class TitleCreateModel :
I have an object model that looks like this (pseudo code): class Product {

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.