I used in EF Expression Like this,
query
.Select(item => new
{
DoubleValue =
new List<string>() { item.AdmissionTicketCode }
.Cast<double>()
.FirstOrDefault()
})
.ToList();
but, run at Unit Test it thorw an error "Specified Cast Is Not Valid".
how do I deal this problem
System.Linq.Enumerable ScreenShot
I tried to add System.Linq.Fakes、and wanted use ShimEnumerable to resolve it, but it does not exist in System.Linq.Fakes.
.Cast<double>()is a NOP on a double and will throw on any other type. Also, keep in mind, that a cast is not a conversion. Ifitem.AdmissionTicketCodeis a string then you need to parse it, not cast it.So let's break down what you might need to do.
Your current code is equivalent to:
Assuming that
item.AdmissionTicketCodeis astringthen the compiler lets us know it's a bad cast right now.So it should be like this:
It's a bit pointless having a single property in an anonymous type, so you could go one step further:
If EF doesn't support
double.Parsethen do this: