[英]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 谢谢
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
; 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.