InvalidCastException: Unable to cast object of type
The full exception reads
InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1[WebApplication1.Models.ListingProjectsDTO]' to type 'System.String'.
The exception points at this method
`
I am not good at casting at all, I have tried return (System.string) obj. I tried to create a list with System.string data type with the help of foreach in the TestDashboard1(). Those of course failed.
Could anyone kindly point me in the right direction?
PS: if anyone is wondering why I am using the generic method here, see https://stackoverflow.com/questions/870697/unable-to-cast-object-of-type-system-dbnull-to-type-system-stringStack Overflow
Unable to cast object of type 'System.DBNull' to type 'System.String`
I got the above error in my app. Here is the original code
public string GetCustomerNumber(Guid id)
{
string accountNumber =
(string)DBSqlHelperFactory.ExecuteScalar(
1 Reply
U give the generic parameter a type 'string' but it expects the type 'ListingProjectsDTO' as generic
So when you call the method, the value 'obj' is of type 'ListingProjectsDTO', but 'T' is a string. You're trying to convert 'ListingProjectsDTO' to a string, which you can't do