Exception for anonymous type in select containing int property
korover opened this issue · 9 comments
Hi,
I am currently getting "Failed to pick matching constructor for type" exception for any int property in anonymous type in the following expression: "Select(x => new { x.Id})". I am using the latest Entity Framework on server side.
It looks strange, since the following combinations work well:
- Select(x => x.Id)
- Select(x => new {Id = (long)x.Id })
Any ideas?
Thanks!
- Any change of re-producing the issue without EF. I assume the exception is thrown client side and EF should have no impact.
- What serialization framework is being used?
- Did you examine the dynamic objects returned from the server before they're transformed? Are type information correct? What types are the values stored in the dynamic properties?
- You are right, no changes without using EF.
- I am using Newtonsoft.Json https://www.newtonsoft.com/json
- If I call DynamicProperty.Values.First().GetType() - I get System.Int64 instead of System.Int32. So the type is incorrect. But I get this incorrect type only on client side, on server side - everything is fine.
You may want to use package aqua-core-newtonsoft-json
and create json serialization settings as following to ensure correct numeric types: new JsonSerializerSettings().ConfigureAqua()
(Or reference package Remote.Linq.Newtonsoft.Json
if using remote linq and call: new JsonSerializerSettings().ConfigureRemoteLinq()
)
Sample:
I am already using new JsonSerializerSettings().ConfigureRemoteLinq())
on both sides.
I have tried the following code:
var result = expression.ExecuteWithEntityFramework(_context);
var jsonSettings = new JsonSerializerSettings().ConfigureRemoteLinq();
var serialized = JsonConvert,SerializeObject(result, jsonSettings);
var type = result.First().Values().First().GetType();
var type2 = JsonConvert.DeserializeObject<IEnumerable<DynamicObject>>(serialized, jsonSettings).First().Values().First().GetType();
I have different values in type(int32) and type2(int64).
I have just found the following info:
Json.NET by default reads integer values as Int64 because there is no way to know whether the value should be Int32 or Int64, and Int64 is less likely to overflow. For a typed property the deserializer knows to convert the Int64 to a Int32 but because your property is untyped you are getting an Int64. [...] It is just the way Json.NET has to work.
It seems such behavior is default for anonymous types deserialization in Json.net.
I was just able to reproduce the issue using the sample solution https://github.com/6bee/Remote.Linq-Samples/tree/master/RemoteQueryable/10_UsingJsonSerializationOverWebApi
This is probably why I was using properties of type long in place of int in contrast to other demos ;-)
Thanks for pointing out, I need to have a deeper look into this...
I have fixed this issue, by using my own custom Json Converter.
Thank you for your help with troubleshooting :)