Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
when try to convert nextid to integer to assign to employeeid
i get error as below
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[System.Int32]' to type 'System.IConvertible'.'

employee id is integer and error show on
int NextIdValue = Convert.ToInt32(nextID);

the above line
how to solve this problem if possible

What I have tried:

<pre>  public ActionResult Next(int id)
        {
            var nextID = _context.GetAll().OrderBy(i => i.EmployeeId)
                     .SkipWhile(i => i.EmployeeId != id)
                     .Skip(1)
                     .Select(i => i.EmployeeId);
            
            ViewBag.NextID = nextID;
           int NextIdValue = Convert.ToInt32(nextID);
            return View("Create", new Employee { EmployeeId = NextIdValue });
         
        }
Posted
Updated 7-Jan-19 22:15pm

If you look closely, the error itself tells you what is wrong.
The parameter you're passing to the method Convert.ToInt32 is of the type.
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[System.Int32]

Try calling
FirstOrDefault()
after the
.Select(i => i.EmployeeId)
statement to get the good type in nextID.
And if it is already an integer, avoid the conversion.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900