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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:55:33+00:00 2026-06-15T14:55:33+00:00

I have 2 objects: public class ClassA { public int Id public string name;

  • 0

I have 2 objects:

public class ClassA
{
   public int Id
   public string name;

   public ClassB myObjectB;

}
public class ClassB
{
   public int Id
   public string name
}

Having 2 Lists for <ClassA> <ClassB>
Some items from List1 match by Id with an item on List2… I want to set the objectB foreach item…

foreach(ClassA item in List1)
{
   ClassB obj = (from b in List2 where b.Id == item.Id select b).SingleOrDefault()
   if(obj != null)
   {
       item.myObjectB = obj; 
       ////////break;    <- ignore this
   }
}

This solution works for me, but I’m just wondering if there is a better way to do this, instead of Foreach

Thanks everyone for your help!!!

  • 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-15T14:55:34+00:00Added an answer on June 15, 2026 at 2:55 pm

    I think that a foreach, in this case, is actually the appropriate approach, as you’re mutating your list. You could potentially simplify your code a bit, however:

    foreach(ClassA item in List1)
    {
        item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id);
    }
    

    This will set the item every time, though it will be set to null if there is no match. If you already have items in myObjectB and setting them to null is inappropriate, you could use:

    foreach(ClassA item in List1)
    {
        item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id) ?? item.myObjectB;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: private static class ClassA{ int id; String name; public ClassA(int
I have a class: public class MyObject { public string Name; public int Age;
I have these objects: public class Class { public Guid Id {get;set;} public string
Assume some domain and view objects (that have no common base class) public class
I have the following objects: @JsonFilter(myFilter) public class Person { private Name name; private
I have two POCO objects: public class Product { public int ProductID { get;
I have a class : public class Person { public string name { get;
My class: public class Device { int ID; string Name; List<Function> Functions; } And
I have the following objects: public class User { int Id { get; set;
First example which works: public class Test { public int ID; public string Name;

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.