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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:59:31+00:00 2026-05-24T08:59:31+00:00

Given an object graph that looks something like the following; A –> B[] –>

  • 0

Given an object graph that looks something like the following;

A --> B[] --> B1[]
          --> B2[]   
  --> C[]

I need to check certain conditions on members/properties of B1 and B2 as well as ensuring that b2.Code appears somewhere within the C[]. If all conditions are satisfied, I then need to construct a new object using variables from elements of the C and B array. My current attempt is shown below, but I am wondering if this could be made more concise with LINQ?

   A a = GetA();
   List<MyTest> tsts = new List<MyTest>();
   foreach (B b in a.B)
    {
        foreach (B1 b1 in b.B1)
        {
            if (b1.State == ("READY"))
            {
                foreach (B2 b2 in b.B2)
                {
                    var tst = (from c in a.C
                           where c.Code == b2.Code && !c.IsRedundant
                           select new MyTest
                           {
                               Code = c.Code,
                               BVal = b.BVal,
                               C1Val = c.C1                     
                           }).FirstOrDefault();
                    if (tst != null)
                        tsts.Add(tst);
                    break;
                }
            }
        }
    }
  • 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-24T08:59:32+00:00Added an answer on May 24, 2026 at 8:59 am

    Absolutely. Basically each new foreach roughly equates to an extra from clause:

    var tests = from b in GetA().B
                from b1 in b.B1
                where b1.State == "READY"
                from b2 in b.B2.Take(1)
                from c in a.C
                           .Where(x => x.Code == b2.Code && !c.IsRedundant)
                           .Take(1)
                select new MyTest
                {
                    Code = c.Code,
                    BVal = b.BVal,
                    C1Val = c.C1                     
                };
    
    var testList = tests.ToList();
    

    A few notes on this:

    • It seems odd that you’re never actually using b1, but you’ll create an extra set of tests for each b1 which is in the “ready” state.
    • The unconditional break within the foreach (B2 b2 ...) loop basically means we only ever execute the loop body once – hence the Take(1)
    • The way you only use the first result of the innermost query is why I’ve got the inner query (expressed through extension methods) with a Take(1) call to get at most one result

    It’s quite possible that some of these oddities can be removed – but it’s not clear what you’re really trying to achieve, so I’ve just tried to make the code reproduce your original query as faithfully as possible.

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

Sidebar

Related Questions

Given an expression that points to a property in an object graph, I want
I have a object graph that is setup like this Clients string Name List[Address]
I have an object model that looks like this (pseudo code): class Product {
How do I check if a given object is nullable in other words how
Do you generally assume that toString() on any given object has a low cost
Given an object like: class M(models.Model): test = models.BooleanField() created_date = models.DateTimeField(auto_now_add=True) Given sample
Given the following models: class Graph(models.Model): owner = models.ForeignKey(User) def __unicode__(self): return u'%d' %
Given two .NET objects (a root and a leaf) in an arbitrary object graph
I would like to automatically determine whether given object is Combinatorica or Mathematica 8.0
Explanation: Let's say I have an object graph that's nested several levels deep and

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.