简体   繁体   English

如何在Entity Framework查询中初始化一个空列表?

[英]How do I initialize an empty list within an Entity Framework query?

I've added a List field to my business models. 我已经将列表字段添加到我的业务模型中。 It's not yet stored in the database, and I was hoping to map it temporarily with something like the following: 它尚未存储在数据库中,我希望使用以下类似内容暂时将其映射:

return MyContext.Foos.Select(foo=> new Foo
    {
        Id = foo.Id,
        Name = foo.Name,
        RequiredFeatures = new List<string>()
    }).ToList();

However, Entity Framework complains that it can't instantiate a new list inside a LINQ to Entities query: 但是,Entity Framework抱怨无法在LINQ to Entities查询中实例化新列表:

A type that implements IEnumerable 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' cannot be initialized in a LINQ to Entities query. 无法在LINQ to Entities查询中初始化实现IEnumerable'System.Collections.Generic.List`1 [[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]的类型。

Basically, List<string> is a complex type, and although it contains a default constructor, the constructor requires some initialization code to be run before the list can be used. 基本上, List<string>是一种复杂类型,尽管它包含默认构造函数,但该构造函数要求先运行一些初始化代码,然后才能使用列表。 Entity Framework just doesn't understand how to generate an empty list from the SQL side of things. 实体框架只是不了解如何从SQL方面生成空列表。

I also tried to achieve the same result with an empty array: 我还尝试使用空数组实现相同的结果:

return MyContext.Foos
    .Select(foo=> new Foo
    {
        Id = foo.Id,
        Name = foo.Name,
        RequiredFeatures = new string[0]
    }).ToList();

The LINQ expression node type 'NewArrayBounds' is not supported in LINQ to Entities. LINQ to Entities不支持LINQ表达式节点类型'NewArrayBounds'。

Again, the expression is not supported by Entity Framework, even though it is much simpler. 同样,即使实体表达式简单得多,实体框架也不支持该表达式。 So I tried the simplest expression of all, for a new array: 因此,我尝试了所有数组中最简单的表达式:

return MyContext.Foos
    .Select(foo=> new Foo
    {
        Id = foo.Id,
        Name = foo.Name,
        RequiredFeatures = { }
    }).ToList();

In constructors and initializers, only property or field parameter bindings are supported in LINQ to Entities. 在构造函数和初始化程序中,LINQ to Entities仅支持属性或字段参数绑定。

I know that one alternative is to load all the objects into memory first, and then initialize the list through C#: 我知道一种替代方法是先将所有对象加载到内存中,然后通过C#初始化列表:

return MyContext.Foos.ToList()
    .Select(foo=> new Foo
    {
        Id = foo.Id,
        Name = foo.Name,
        RequiredFeatures = new List<string>()
    }).ToList();

However, this seems like a workaround, not an actual solution. 但是,这似乎是一种解决方法,而不是实际的解决方案。 If I don't want to re-iterate over the collection, how can I initialize the list? 如果我不想在集合上重复,如何初始化列表?

Surprisingly, although new string[0] and { } do not work, new string[] { } and new string[0] { } do! 令人惊讶的是,尽管new string[0]{ }不起作用,但是new string[] { }new string[0] { }起作用了!

return MyContext.Foos.ToList()
    .Select(foo=> new Foo
    {
        Id = foo.Id,
        Name = foo.Name,
        RequiredFeatures = new string[] { }
    }).ToList();

However, this does have the caveat that you can't add items to the list, as it is backed by an array, not an actual list. 但是,这确实有一个警告:您不能向列表中添加项目,因为它由数组(而不是实际列表)支持。 They all seem functionally identical, and I'm not sure why Entity Framework supports some but not the others. 它们在功能上似乎都相同,我不确定为什么实体框架支持某些而不支持其他。

Empty collections 空集合

Supported: 支持的:

  • new string[] { }
  • new string[0] { }

Not supported: 不支持:

  • { }
  • new string[0]
  • new List<string>()
  • new List<string> { }
  • new List<string>() { }

Non-empty collections 非空集合

Supported: 支持的:

  • new [] { "foo" }
  • new string[] { "foo" }
  • new string[1] { "foo" }
  • new List<string> { "foo" }
  • new List<string>() { "foo" }

Not supported: 不支持:

  • { "foo" }

It is interesting that Entity Framework supports a list with values, but not an empty list. 有趣的是,Entity Framework支持带有值的列表,但不包含空列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM