LINQ to Entities无法识别NotSupportedException方法

时间:2016-06-02 08:53:05

标签: c# entity-framework linq

LINQ to EF:

 db.ReadonlyQuery<Transaction>()
     .Select(t => new ACurrentDayInfo
         {
              OrderId = t.TransactionIdentifier,
              OrderTime = t.TransactionTime,
              UserName = JsonConvert.DeserializeObject<UserInfo>(t.UserInfo).RealName ?? ""
         })
     .ToListAsync();

t.UserInfo是表格字段中每个记录中的{"RealName ":"XY"}的json字符串。 如何将其转换为服务器查询?

2 个答案:

答案 0 :(得分:2)

使用像这样的getter扩展你的ACurrentDayInfo课程

class  ACurrentDayInfo
{
    public string UserName
    {
        get
        {
            return JsonConvert.DeserializeObject<UserInfo>(UserInfo).RealName ?? "";
        }
    }
}

并修改您的查询:

db.ReadonlyQuery<Transaction>()
                    .Select(t => new ACurrentDayInfo
                    {
                        OrderId = t.TransactionIdentifier,
                        OrderTime = t.TransactionTime,
                        UserInfo = t.UserInfo
                    }).ToListAsync();

答案 1 :(得分:0)

在EF Dbcontext中,不支持在查询中使用cast json,你需要修复相同的内容:

var listData = db.ReadonlyQuery<Transaction>()
                        .Select(t => new ACurrentDayInfo
                        {
                            OrderId = t.TransactionIdentifier,
                            OrderTime = t.TransactionTime,

                            UserInfo = t.UserInfo
                        }).ToListAsync();

foreach (var item in listData)
{
    item.UserName = JsonConvert.DeserializeObject<UserInfo>(t.UserInfo).RealName ?? "" 
}