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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:41:20+00:00 2026-06-17T07:41:20+00:00

I have any array of ( Pilot ) objects with a ( Hanger )

  • 0

I have any array of (Pilot) objects with a (Hanger) property, which may be null, which itself has a (List<Plane>) property. For testing purposes, I want to simplify and ‘flatten’ this to an anonymous object with properties PilotName (string) and Planes (array) but not sure how to handle a null Hanger property or an empty PlanesList.

(Why anonymous objects? Because the objects of the API I’m testing are read only and I want the test to be ‘declarative’: self contained, simple and readable… but I’m open to other suggestions. Also I’m trying to learn more about LINQ.)

example

class Pilot
{
    public string Name;
    public Hanger Hanger;
}

class Hanger
{
    public string Name;
    public List<Plane> PlaneList;
}

class Plane
{
    public string Name;
}

[TestFixture]
class General
{
    [Test]
    public void Test()
    {
        var pilots = new Pilot[]
        {
            new Pilot() { Name = "Higgins" },
            new Pilot()
            {
                Name = "Jones", Hanger = new Hanger()
                {
                    Name = "Area 51",
                    PlaneList = new List<Plane>()
                    {
                        new Plane { Name = "B-52" },
                        new Plane { Name = "F-14" }
                    }
                }
            }
        };

        var actual = pilots.Select(p => new
        {
            PilotName = p.Name,
            Planes = (p.Hanger == null || p.Hanger.PlaneList.Count == 0) ? null : p.Hanger.PlaneList.Select(h => ne
            {
                PlaneName = h.Name
            }).ToArray()
        }).ToArray();

        var expected = new[] {
            new { PilotName = "Higgins", Planes = null },
            new
            {
                PilotName = "Jones",
                Planes = new[] {
                    new { PlaneName = "B-52" },
                    new { PlaneName = "F-14" }
                }
            }
        };

        Assert.That(actual, Is.EqualTo(expected));
    }

The immediate problem is that the line expected... Planes = null errors with,

Cannot assign to anonymous type property but admit the underlying problem may be that using null in actual is using null is not the best approach in the first place.

Any ideas how to either assign the null array in expected or take a different approach than null in actual?

  • 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-17T07:41:21+00:00Added an answer on June 17, 2026 at 7:41 am

    There are two things happening:

    Firstly, when you construct an instance of an anonymous type using new { Name = Value}, in order to build the type the compiler needs to be able to work out the type of Value. Just null on its own doesn’t have a type, so the compiler wouldn’t know what type to give your Planes member.

    Now, if you were using a named type for the value, you could just say (type)null and be done, BUT because you want an array of another anonymous type, there’s no way to refer to is (it’s anonymous!).

    So how do you get null typed as array of an anonymous type? Well, the C# spec guarantees that anonymous types with members the same names and types (in the same order!) are unified; that is, if we say

    var a = new { Foo = "Bar" };
    var b = new { Foo = "Baz" };
    

    then a and b have the same type. We can use this fact to get our suitably-typed null thus:

    var array = (new[] { new { PlaneName = "" } });
    array = null;
    

    It’s not pretty but it works – now array has the right type but a null value. So this compiles:

    var array = new[] {
      new {
        PlaneName = ""
      }
    };
    array = null;
    
    var expected = new[] {
      new {
        PilotName = "Higgins",
        Planes = array
      },
      new {
        PilotName = "Higgins",
        Planes = new[] {
          new {
            PlaneName = "B-52"
          },
          new {
            PlaneName = "F-14"
          }
        }
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct array called AnalysisResults , that may contain any MATLAB datatypes,
Does anyone know do there have any way that I can encrypt the array
I've an array of fixed size in C . There I might have any
I have an array of any type T ( T[] ) and I want
I have an array that contains an array of arrays if that makes any
Is there any method like the array_unique for objects? I have a bunch of
Is there any way to obtain Object array of Java Bean fields? I have
I have an array which I'm doing some calculations on. The array begins as
I have a huge array of 2D points (about 3 millions of pairs), which
I have some code which generates an array; at each index there is an

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.