Search code examples
c#linqvisual-studio-2010deferred-execution

Unable to cast object of type 'System.Int32' to type 'System.String' in LINQ query


Hi I'm trying to remove a user from my database. I'm not sure why I'm receiving this error though since none of my variables (employeeName, departmentName) are of type int.

The debugger keeps stopping at this block of code with the "Unable to cast object of type 'System.Int32' to type 'System.String'." error.

//Note: employeeName type is navchar
        var empQuery =
            from dep in db.Department
            join emp in db.EmployeeDetails
            on dep.departmentId equals emp.departmentId
            where dep.departmentName == reqDep
            && emp.employeeName == reqUser
            select emp;

        //Debugger keeps pointing to the foreach with the error
        foreach (var element in empQuery)
        {
            Console.WriteLine("user: " + element + " has been deleted");
            db.EmployeeDetails.DeleteOnSubmit(element);
        }
        // Freeze the console window.
        Console.ReadLine();

If it helps, I've included the entire code below.

namespace runLinqSql
{
// Table<T> abstracts database details per table/data type. 
[Database]
public class FireEvacuation : DataContext
{
    public Table<Employee> EmployeeDetails;
    public Table<EmpDepartment> Department;

    public FireEvacuation(string connection) : base(connection) { }
}
  class Program
{
    static void Main(string[] args)
    {
        // Use a connection string.
        FireEvacuation db = new FireEvacuation
            (@"C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\runSqlLinq\FireEvacuation.mdf");


        ////specify department to be removed
        string reqDep = "KOM";
        // Attach the log to show generated SQL.
        db.Log = Console.Out;

        ////specify user to be removed
        string reqUser = "Jason";


        //Note: employeeName type is navchar
        var empQuery =
            from dep in db.Department
            join emp in db.EmployeeDetails
            on dep.departmentId equals emp.departmentId
            where dep.departmentName == reqDep
            && emp.employeeName == reqUser
            select emp;

        //Debugger keeps pointing to the foreach with the error
        foreach (var element in empQuery)
        {
            Console.WriteLine("user: " + element + " has been deleted");
            db.EmployeeDetails.DeleteOnSubmit(element);
        }
        // Freeze the console window.
        Console.ReadLine();

        try
        {
            db.SubmitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            // Provide for exceptions.
        }

Solution

  • Seems to me that the error has something to do with one of the following lines:

    on dep.departmentId equals emp.departmentId
    where dep.departmentName == reqDep
    && emp.employeeName == reqUser
    

    (The reason the exception is being thrown at the loop, is that the expression will not have been run before that, which is the first time it is needed).

    You are comparing values in pairs here. Now if one of the values in a pair is of type string, and the other is of type int, that will cause that error.

    To fix this, double check that each pair you are comparing are really valid (i.e. that dep.departmentId and emp.departmentId are really what you want to compare), and that the types are the same.

    If the types are not the same, but they can still be expected re return equivalent values (example: dep.departmentId could be an int value of 123, while emp.departmentId is the string "123"), you can probably compare them both as strings:

    on dep.departmentId.ToString() equals emp.departmentId.ToString()