LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)', so the method cannot be converted to a stored expression

When finding the maximum value of an all-digit character column or sorting the column numerically, the following syntax will throw an error in the title:

context.Table.Max(p => Convert.ToInt32(p.Id));

Before Entity Framework 4.0, we could write this without error:

context.Table.Select(p => p.Id).ToArray().Max(p => Convert.ToInt32(p))


After Entity Framework 4.0, we can directly execute SQL statements, which are written as follows:

context.ExecuteStoreQuery<Int32>("select max(convert(int,Id)) from Table").First();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325489410&siteId=291194637