无法将匿名类型#1 []隐式转换为匿名类型#2 []

比什努·拉瓦尔(Bishnu Rawal)

我有以下情况:

public JsonResult ChangeFilterList(int option)
{
    var data = new[] { new { Text = "Unknown option", Value = -1 } };
    switch (option)
    {
        case 2: data = _departmentnameRepository.All.Select(x => new { Text = x.DeptName, Value = x.Id }).ToArray();
            break;
        case 3: data = Session["projectid"] == null
                ? _assetSequenceRepository.All.Select(x => new { Text = x.AssetShotName, Value = x.Id }).ToArray()
                : _assetSequenceRepository.FindBy(p => p.ProjectId == (int)Session["projectid"]).Select(x => new { Text = x.AssetShotName, Value = x.Id }).ToArray();
            break;
        default: data = _userRepository.All.Select(x => new { Text = x.DisplayName, Value = x.UserID }).ToArray();
            break;
    }            

    return Json(data, JsonRequestBehavior.AllowGet);
}

case2default看上去很不错,但抱怨的情况下3(有条件)说:Cannot implicitly convert type 'AnonymousType#1[]' to 'AnonymousType#2[]'?:由于我已经提供了匿名用户的蓝图,因此应该无法确定类型var data = new[] { new { Text = "Unknown option", Value = -1 } };

解决方案:

@Darin Dimitrov的答案很好,但我想对匿名类型进行一些测试(简单的案例总是需要它)。正如@道格拉斯(Douglas)所怀疑的:我assetSequenceRepository提供的idlong和匿名Value赞成int不提供的long由于C#编译器没有隐式转换longint,所以出现了错误。编译代码段为:

public JsonResult ChangeFilterList(int option = 3)
        {
            var data = new[] { new { Text = "Unknown option", Value = long.MaxValue } };
            switch (option)
            {
                case 2: data = _departmentnameRepository.All.Select(x => new { Text = x.DeptName, Value = (long)x.Id }).ToArray();
                    break;
                case 3: data = Session["projectid"] == null
                        ? _assetSequenceRepository.All.Select(x => new { Text = x.AssetShotName, Value = x.Id }).ToArray()
                        : _assetSequenceRepository.FindBy(p => p.ProjectId == (int)Session["projectid"]).Select(x => new { Text = x.AssetShotName, Value = x.Id }).ToArray();
                    break;
                default: data = _userRepository.All.Select(x => new { Text = x.DisplayName, Value = (long)x.UserID }).ToArray();
                    break;
            }            

            return Json(data, JsonRequestBehavior.AllowGet);
        }
道格拉斯

我的猜测是,您的FindBy方法返回的对象的属性与您期望的对象具有不同的类型(例如int?而不是int)。尝试指定类型转换,以确保您的匿名类型具有正确的定义:

case 3: data = Session["projectid"] == null
             ? _assetSequenceRepository.All.Select(x => new { Text = x.AssetShotName, Value = x.Id }).ToArray()
             : _assetSequenceRepository.FindBy(p => p.ProjectId == (int)Session["projectid"]).Select(x => new { Text = (string)x.AssetShotName, Value = (int)x.Id }).ToArray();
        break;

关键更改是:

new { Text = (string)x.AssetShotName, Value = (int)x.Id })
                  ↖    explicit type casts    ↗

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法隐式将匿名类型#1转换为对象

无法隐式转换类型列表匿名类型1 linq

无法将类型List <<匿名类型:.. >>隐式转换为IEnumerable <Product>

无法将匿名类型隐式转换为 System.Collections.Generic.List

无法将类型“System.Collections.Generic.List<<匿名类型:>>”隐式转换为“System.Collections.Generic.List”

无法将类型 *** 隐式转换为 ****

无法将类型'.List <AnonymousType#1>'隐式转换为'.List <WebApplication2.Customer>'

Web API 无法隐式转换类型“System.Collections.Generic.List<<匿名类型:

无法隐式转换类型“System.Collections.Generic.List<<匿名类型

无法将匿名方法转换为类型“委托”

2错误-无法将类型system.collection.generic.list <float>隐式转换为float,也无法将float []类型隐式转换为float

将匿名类型转换为字典

将JObject转换为匿名类型

无法将类型DbSet <>隐式转换为IQueryable <>

无法将类型“ void”隐式转换为“ x”

无法将类型[具体]隐式转换为[接口]

无法将类型'string'隐式转换为'decimal'

无法将类型'int'隐式转换为'bool'Unity

无法将类型'double'隐式转换为'int'。-错误

无法将类型ResponseBase隐式转换为Response

无法隐式将类型string []转换为string

无法将类型“ double”隐式转换为“ int”

无法将类型ApplicationUser隐式转换为User

无法将类型 'IEnumerable<int> 隐式转换为 'int'

无法将类型“int”隐式转换为“bool”

无法将类型int []隐式转换为int?[]

无法将类型'float'隐式转换为'int'

“无法将类型'bool []'隐式转换为'object []'”

无法将类型'bool'隐式转换为'float'