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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:11:53+00:00 2026-06-18T09:11:53+00:00

I have 2 collections of 2 different types but have almost the same set

  • 0

I have 2 collections of 2 different types but have almost the same set of fields.
in one function, I need to iterate through one of the collections depending on one condition.
I want to write only one code block that will cover both cases.
Example:
I have the following code:

if (condition1)
{
    foreach(var type1var in Type1Collection)
    {

    // Do some code here
       type1var.Notes = "note";
       type1var.Price = 1;
    }
}
else
{
    foreach(var type2var in Type2Collection)
    {

    // the same code logic is used here
       type2var.Notes = "note";        
       type2var.Price = 1;
    }
}

Now: I want to simplify this code to use the same logic only once ( as they are identical ), something like the following ( P.S : I know the following code is not correct, I am just explaining what I want to do ):

var typecollection = Condition1 ? Type1Collection : Type2Collection;

foreach(var typevar in TypeCollection)
{

   // the same code logic is used here
   typevar.Notes = "note";
   typevar.Price = 1;       
 }

The definition of Type1 & Type2 is similar to the following code ( Actually they are Entity objects):

    public class Type1 : EntityObject
    {
        public int Type1ID { get; set; }
        public int Type1MasterID { get; set; }

        public String Notes { get; set; }
        public decimal Price { get; set; }
    }

    public class Type2 : EntityObject
    {
        public int Type2ID { get; set; }
        public int Type2MasterID { get; set; }

        public String Notes { get; set; }
        public decimal Price { get; set; }
    }

Update 1:

I have included some sample code I am using inside foreach block ( I am accessing a public properties of the 2 types).

Update 2:

I have included sample Type1 & Type2 definitions, as you can see I have 2 common Public Properties in both classes which I want to update in foreach block.

Update 3:

I am sorry for the confusion, Type1 & Type2 are derived from EntityObject ( They are both part of my entity model, and the Type1Collection & Type2Collection are actually EntityCollection of these 2 entities.

  • 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-18T09:11:54+00:00Added an answer on June 18, 2026 at 9:11 am

    Given Jon Skeet’s hint of using LINQ’s Concat method and the OP’s statement that the classes involved are EntityObjects, here’s another possible solution. This assumes that the EntityObject subclasses are defined as partial classes:

    public partial class Type1 : EntityObject
    {
        public int Type1ID { get; set; }
        public int Type1MasterID { get; set; }
        public String Notes { get; set; }
        public decimal Price { get; set; }
    }
    
    public partial class Type2 : EntityObject
    {
        public int Type2ID { get; set; }
        public int Type2MasterID { get; set; }
    
        public String Notes { get; set; }
        public decimal Price { get; set; }
    }
    

    This allows the OP to declare an interface with the common properties, and have his EntityObject subclasses implement that interface:

    public interface IMyType
    {
        String Notes { get; set; }
        decimal Price { get; set; }
    }
    public partial class Type1 : IMyType {}
    public partial class Type2 : IMyType {}
    

    And the original code becomes:

    var query = (
        from type1var in type1Collection
        where condition1
        select (IMyType)type1var
       ).Concat(
        from type2var in type2Collection
        where !condition1
        select (IMyType)type2var
       );
    foreach(var myType in query)
    {
        myType.Notes = "note";
        myType.Price = 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which I need to call for three different types, with
I have three different List collections. How can I show them in one ListBox
Through the last years I have different types of software to organize my photo
I have a set of documents that are the same but could be sorted
Can I contain two different types in a collection? For example, can I have
I have 2 collections, one of available features and one of user features. I
I have 2 collections of caracter string ex: List<string> one = new List<string>; one.Add(a);
I have 2 different object types stored in RavenDb, which are a parent/child type
I have two different collections: List<int> IEnumerable<IDataRecord> The first contains a list of primary
I have a collection class that holds lots of different types of data in

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.