40

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?

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
petemoloy
  • 695
  • 1
  • 5
  • 9
  • 1
    The primary issue with your updated example is because you're checking equality between an enumerator (the `WhereSelectArrayIterator` created by the `pilots.Where` call) and an array. You can use `ToArray()` at the end of the `Where` clause to force it into an array. – Andras Zoltan Jan 14 '13 at 11:30
  • To the wider problem I'd say two things: Firstly, A very good change to consider would be making (eg) `Planes` be an **empty collection** rather than **`null`**, when there aren't any items. It's much easier to deal with; [here's some background reading](http://stackoverflow.com/questions/1969993). Secondly, I suspect you're going to have to make the equality check between actual and expected more 'intelligent'; ie, it's going to have to dive into the objects and collections, and use something like `SequenceEqual`. – AakashM Jan 14 '13 at 11:32
  • @AndrasZoltan: You are right, question now corrected :) – petemoloy Jan 14 '13 at 11:54
  • @AakashM: empty collection: I did the background reading and I agree but I can't get the required change right within my code. Can you suggest a fix? Thanks. p.s. you are correct about the Assert too :) – petemoloy Jan 14 '13 at 11:56
  • Hmm, of course with a sometimes-`null` `Hanger` it's harder... maybe [Null Object Pattern](http://en.wikipedia.org/wiki/Null_Object_pattern#C.23) for that. For collections, just return `Enumerable.Empty` rather than `null`. – AakashM Jan 14 '13 at 12:02
  • @AakashM: I've appended my attempt to the end of the question but has a compile error. Ccan you suggest a fix? Thanks again. – petemoloy Jan 14 '13 at 12:27
  • @petemoloy With that, the two objects in the array are actually different types, because while `Enumerable.Empty()` and `List` both implement `IEnumerable`, they're not the exact same type. Put `.AsEnumerable()` after the `new List<>()` and it compiles – AakashM Jan 14 '13 at 12:30

3 Answers3

81

You have to use a typed null:

(List<Plane>)null

Or

(Plane[])null

Otherwise the compiler has no idea what type you want the anonymous type's member to be.

Update As @AakashM has rightly pointed out - this solves your problem of assigning a null to an anonymous member - but doesn't actually compile - and if it did it wouldn't allow you to refer to these members.

A fix would be to do this (unfortunately both the null and the anonymous Planes array will need casting:

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

So use IEnumerable as the member type. You could also use IEnumerable<object> but the effect will be the same either way.

Or - you could use IEnumerable<dynamic> as the common type - this would let you do this:

Assert.AreEqual("B-52", expected[1].Planes.First().PlaneName);
Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
42

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"
      }
    }
  }
};
sschwei1
  • 362
  • 2
  • 11
AakashM
  • 62,551
  • 17
  • 151
  • 186
14

Just use default(Plane[]) instead of null.

Viacheslav Ivanov
  • 1,535
  • 13
  • 22