0
votes

I have this piece of code that's been throwing a casting error, and I have no idea why after playing with it for half an hour. This is the piece of code:

var zips = DB.Get<Partner_to_ZipCode>()
                .AsQueryable()
                .Include(i => i.Partner)
                .Where(w => w.Partner.Id == id)
                .Select(s => s.ZipCode)
                .ToArray();

It's pointing to line 146 of the controller, which is this linq query right here. "Partner.Id" is an integer as is "id", and ZipCode is a string. I have no idea what's going on here. I've tried commenting out pieces of code, casting variables, changing the array from a generic to a specific type, none of it did anything. Any help would be appreciated.

The error message is:

{"message":"An error has occurred.","exceptionMessage":"The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid.","exceptionType":"System.InvalidOperationException","stackTrace":" at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()\r\n at System.Linq.Buffer`1..ctor(IEnumerable`1 source)\r\n at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)\r\n at PMP.MVC.Controllers.PartnerController.GetTasks(Int32 id) in c:\\Users\\maran\\OneDrive\\Documents\\RF\\main\\src\\PMP.MVC\\PMP.MVC\\Controllers\\PartnerController.cs:line 146\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ExceptionFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Web.Http.Controllers.ExceptionFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

2
Mismatch between database column types and entity types?Chris Pickford
Assign each Linq chain to an individual variable/Linq.Function() to add transparency into what's going on where.HouseCat
.Where(w => w.Partner.Id == id) maybe there you compare int to stringdaniell89
Both Ids are integers, the entity and db types match. I have no idea what's going on.tfossdq

2 Answers

0
votes

one of these two might work:

.Where(w => w.Partner.Id.ToString() == id)

.Where(w => w.Partner.Id == id.ToString())

one of those: local variable "id" or w.Partner.Id is a string and the other is a int.
It is safer to cast int to string than other way, because string might contain something else than numbers and you'll need to handle that case.

0
votes
var zips = DB.Get<Partner_to_ZipCode>()
.AsQueryable()
.Include(i => i.Partner)
.Where(w => w.Partner.Id.ToString() == id) // or (w => w.Partner.Id == int.Parse(id)) if id is an integer
.Select(s => s.ZipCode)
.ToArray();