简体   繁体   English

EF错误无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型

[英]EF error Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context

I m querying some collections and then converting my result to JSON but getting below error: 我正在查询一些集合,然后将结果转换为JSON,但出现以下错误:

Unable to create a constant value of type 'Anonymous type'. 无法创建类型为“匿名类型”的常量值。 Only primitive types or enumeration types are supported in this context 在此上下文中仅支持原始类型或枚举类型

Below is my code, please guide & help me. 以下是我的代码,请指导并帮助我。

  var AllStatus = RepositoryFactory.OrderStatusRepository.GetAll().AsEnumerable().Select(s => new
            {
              Status =  s.Status,
               StatusId= s.Id
            });

            var AllUsers = RepositoryFactory.UserRepository.GetAll().AsEnumerable().Select(u => new
            {
                UserId= u.UserId,UserName=u.UserName 
            });

            var result = RepositoryFactory.OrderHistoryRepository.GetAll().Select(v => new
            {
                PatientId = v.UserId ,
                UserName = AllUsers.Where(u=>u.UserId==v.UserId).Select(u=>u.UserName),
                Status = AllStatus.Where(s=>s.StatusId==v.OrderStatusId).Select(s=>s.Status),
                StatusDate = v.UpdatedDate,
                Amount = v.Amount  
            }) ; 


            returnModel.Data = result.ToJSON();

thanks 谢谢

  1. Parts of code: AllUsers.Where(u=>u.UserId==v.UserId).Select(u=>u.UserName) and AllStatus.Where(s=>s.StatusId==v.OrderStatusId).Select(s=>s.Status) return IEnumerable<string> , and you need, I guess, single string ; 代码部分: AllUsers.Where(u=>u.UserId==v.UserId).Select(u=>u.UserName)AllStatus.Where(s=>s.StatusId==v.OrderStatusId).Select(s=>s.Status)返回IEnumerable<string> ,我想您需要单个string
  2. Instead Select method, use ToDictionary ToDictionary使用Select方法,使用ToDictionary
var AllStatus = RepositoryFactory.OrderStatusRepository.GetAll().AsEnumerable()
               .ToDictionary(x=> x.Id, x=> x.Status);

var AllUsers = RepositoryFactory.UserRepository.GetAll().AsEnumerable()
              .ToDictionary(u => u.UserId, u=> u.UserName);

var result = RepositoryFactory.OrderHistoryRepository.GetAll()
        .Select(v => new
        {
            PatientId = v.UserId ,
            UserName = AllUsers.ContainsKey(v.UserId) ? AllUsers[v.UserId] : null,
            Status = AllStatus.ContainsKey(v.StatusId) ? AllUsers[v.StatusId] : null,
            StatusDate = v.UpdatedDate,
            Amount = v.Amount  
        }) ;

暂无
暂无

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

相关问题 EntityFramework无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型 - EntityFramework Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context 无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context EF-无法创建类型的常量值在此上下文中仅支持原始类型或枚举类型 - EF - Unable to create a constant value of type Only primitive types or enumeration types are supported in this context EF6 Lambda查询错误:无法创建类型常量...在此上下文中仅支持枚举类型的原始类型 - EF6 Lambda Query Error: Unable to create a constant of type … Only primitive types of enumeration types are supported in this context 错误:无法创建类型为“ System.Object”的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context LINQ查询错误:无法创建类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - LINQ Query error: Unable to create a constant value of type. Only primitive types or enumeration types are supported in this context 错误:无法创建类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型。 在Linq C#中 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context. in Linq C# 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型两个db Linq查询 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context two db Linq query 无法创建类型“”的常量值。 在此上下文中仅支持原始类型或枚举类型。 ASP.NET MVC LINQ EF - Unable to create a constant value of type ''. Only primitive types or enumeration types are supported in this context. ASP.NET MVC LINQ EF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM