LINQ to Entities 不支持 LINQ 表达式节点类型“NewArrayBounds”

问题描述 投票:0回答:2
 select new ProviderMeta
            {
                LoginId = user.LoginId,
                AgencyId = user.AgencyId,
                Application = user.Application,
                UserId = user.UserId,
                Name = agencySnapshot.Name,
                Roles = new int[0],
                Cluster = app.ClusterId ?? 0,
                Created = app.Created,
                TitleType = user.TitleType,
                Feature = (foundFeature == null ? 0 : foundFeature.Feature)
            }).ToList();

这里,Roles 是一个整数数组,但它不允许我分配一个空数组 与零。 如有帮助,我们将不胜感激。

c# arrays entity-framework linq linq-to-entities
2个回答
1
投票

在类的空构造函数中初始化数组:

public class ProviderMeta
{
    //...
   public ProviderMeta()
   {
     Roles = new int[]{0};
   }
}

并将其从投影中移除

 select new ProviderMeta
        {
            LoginId = user.LoginId,
            AgencyId = user.AgencyId,
            Application = user.Application,
            UserId = user.UserId,
            Name = agencySnapshot.Name,
            //Roles = new int[0], remove this line
            Cluster = app.ClusterId ?? 0,
            Created = app.Created,
            TitleType = user.TitleType,
            Feature = (foundFeature == null ? 0 : foundFeature.Feature)
        }).ToList();

0
投票

您可以选择一个空数组进行初始化:

new string[] {}

这与选择包含项目的数组时的效果相同:

 IList<Bar> empty = context.Foo.Select(foo => new Bar
 {
     // ...
     Roles = new int[] {},
     // ...
 }).ToList();

 IList<Bar> withRoles = context.Foo.Select(foo => new Bar
 {
     // ...
     Roles = new [] { 1, 2, 3},
     // ...
 }).ToList();
© www.soinside.com 2019 - 2024. All rights reserved.