YUu
YUu

Reputation: 79

LINQ to Entities does not recognize the method NotSupportedException

LINQ to EF:

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

The t.UserInfo is a json string like {"RealName ":"XY"} in each record of in table's field. How can I translate this to a server query?

Upvotes: 4

Views: 942

Answers (2)

Linh Tuan
Linh Tuan

Reputation: 448

In EF Dbcontext don't support cast json in query, You need fix same that:

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 ?? "" 
}

Upvotes: 0

Peter Bons
Peter Bons

Reputation: 29780

Extend you ACurrentDayInfo class with a getter like this

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

and modify your query like this:

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

Upvotes: 2

Related Questions