c# - 无法将 null 分配给类型数组的匿名属性

标签 c# linq anonymous-types

我有任何具有(Pilot)属性的(Hanger)对象数组,该属性可能为空,它本身具有(List<Plane>)属性。出于测试目的,我想将其简化并“扁平化”为具有属性 PilotName 的匿名对象(字符串)和 Planes (数组)但不确定如何处理 null Hanger属性或空 PlanesList .

(为什么是匿名对象?因为我正在测试的 API 对象是只读的,我希望测试是“声明性的”:自包含、简单且可读......但我愿意接受其他建议。此外,我正在尝试了解有关 LINQ 的更多信息。)

例子

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));
    }

眼前的问题是 expected... Planes = null 行错误,

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.

关于如何在 expected 中分配空数组的任何想法或采取与 null 不同的方法在actual

最佳答案

您必须使用typed null:

(List<Plane>)null

或者

(Plane[])null

否则编译器不知道您希望匿名类型的成员是什么类型。

更新 正如@AakashM 正确指出的那样 - 这解决了您分配 null 的问题给一个匿名成员——但实际上并没有编译——如果编译了,它就不允许你引用这些成员。

解决方法是这样做(不幸的是 null 和匿名 Planes 数组都需要转换:

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

所以使用IEnumerable作为成员类型。您也可以使用 IEnumerable<object>但无论哪种方式,效果都是一样的。

或者 - 你可以使用 IEnumerable<dynamic>作为普通类型 - 这会让你这样做:

Assert.AreEqual("B-52", expected[1].Planes.First().PlaneName);

关于c# - 无法将 null 分配给类型数组的匿名属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14317161/

相关文章:

c# - 发送 SqlParameter 给 Dapper

c# - Mongodb 驱动程序 Linq 查询抛出异常

c# - 在 C# 中应该如何使用匿名类型?

具有属性的类实例化的 C# 到 VB.NET 语法转换

c# - 创建一个匿名对象,该对象必须在 Key 名称中包含点并将其放入另一个匿名对象中

c# - 请帮助我理解这个委托(delegate)示例

时间:2019-03-17 标签:c#Linq with complex result

c# - 声明 IEnumerable<T> 变量以供稍后使用

c# - Nhibernate 抛出类型为 'System.Linq.EnumerableQuery` 的对象 1[实体 ]' cannot be converted to type ' System.Linq.IQueryable`1[System.Object[]]'

c# - 强制我的代码使用我的扩展方法