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字符串。
如何将其转换为服务器查询?
答案 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 ?? ""
}