Operator '==' cannot be applied to operands of type 'int' and 'string' error

nouptime

Why do I see the aforementioned error with this bit of code? The error specifically occurs in the t.TerritoryID == territoryID section:

[HttpPost]
public ActionResult Add(EmployeeViewModel employee, string[] territories)
{
        ModelState.Remove("territories");

        if (ModelState.IsValid)
        {
            if (territories != null)
            {
                employee.Territories = territories.Select(territoryID => repository.Territories.FirstOrDefault(t => t.TerritoryID == territoryID));
            }

            employee.EmployeeID = repository.CreateEmployee(employee);
        }

        return RedirectToAction("Index");
}

In case you're wondering, TerritoryID is an integer. Any suggestions on how I can rectify this issue? Thanks in advance.

fejesjoco

Int and string are two different types, you can't directly compare them. You can convert an int to a string or vice versa.

Change territories to an int array: var territoriesInt = territories.Select(x => int.Parse(x)).ToArray(), and then use that.

The above code is string to int conversion. This can fail if a string doesn't contain a number. I prefer to let it fail on purpose if someone sends bad data. It can be done the opposite way, too, if you compare your territories strings to territoryID.ToString().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Operator '==' cannot be applied to operands of type 'char' and 'string'

From Dev

Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’

From Dev

C#: operator '-' cannot be applied to operands of type 'string' and 'int' error

From Dev

Operator '>=' cannot be applied to operands of type 'System.DateTime?' and 'int'

From Dev

ASP.NET C# error: Operator * cannot be applied to operands of type 'int' and 'string'

From Dev

"Operator '==' cannot be applied to operands of type 'char' and 'string'"

From Dev

Binary operator '|' cannot be applied to operands of type 'Int' and 'UInt8'

From Dev

Binary operator * cannot be applied to operands of type Int and CGFloat

From Dev

Operator >= cannot be applied to operands of type string and datetime

From Dev

Binary Operator + Cannot be Applied to Operands of Type CGfloat int

From Dev

Operator '!=' cannot be applied to operands of type 'Task' and 'int'

From Dev

Binary operator '..<' cannot be applied to operands of type 'Int' and 'CGFloat'

From Dev

Operator '==' cannot be applied to operands of type string and Enum

From Dev

Why am I getting the error message "operator cannot be applied to operands of type string and int"?

From Dev

Operator '>=' cannot be applied to operands of type 'System.DateTime?' and 'int'

From Dev

Operator '-' cannot be applied to operands of type 'float' and 'string'

From Dev

operator <= cannot be applied to operands of type string and string

From Dev

Operator + cannot be applied to operands of type IntPtr and int - .Net 3.5

From Dev

Binary operator '|' cannot be applied to operands of type 'Int' and 'UInt8'

From Dev

Operator '==' cannot be applied to operands of type

From Dev

operator '||' cannot be applied to operands of type string and bool

From Dev

C# ERROR Operator * cannot be applied to operands of type 'string' and 'string'

From Dev

Operator '==' cannot be applied to operands of type string and char

From Dev

Error Message: Operator '+' cannot be applied to operands of type 'string' and 'method group'

From Dev

Operator '>' cannot be applied to operands of type 'object' and 'string'

From Dev

Error comparing Integers: Binary operator '<=' cannot be applied to operands of type 'Int?' and 'Int'

From Dev

Function error: "Binary operator '+' cannot be applied to operands of type 'Double' and 'Int'"

From Dev

Binary operator '..<' cannot be applied to operands of type 'Int' and 'Int?'

From Dev

Error : Binary operator '-=' cannot be applied to operands of type 'Int?' and 'Int'

Related Related

  1. 1

    Operator '==' cannot be applied to operands of type 'char' and 'string'

  2. 2

    Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’

  3. 3

    C#: operator '-' cannot be applied to operands of type 'string' and 'int' error

  4. 4

    Operator '>=' cannot be applied to operands of type 'System.DateTime?' and 'int'

  5. 5

    ASP.NET C# error: Operator * cannot be applied to operands of type 'int' and 'string'

  6. 6

    "Operator '==' cannot be applied to operands of type 'char' and 'string'"

  7. 7

    Binary operator '|' cannot be applied to operands of type 'Int' and 'UInt8'

  8. 8

    Binary operator * cannot be applied to operands of type Int and CGFloat

  9. 9

    Operator >= cannot be applied to operands of type string and datetime

  10. 10

    Binary Operator + Cannot be Applied to Operands of Type CGfloat int

  11. 11

    Operator '!=' cannot be applied to operands of type 'Task' and 'int'

  12. 12

    Binary operator '..<' cannot be applied to operands of type 'Int' and 'CGFloat'

  13. 13

    Operator '==' cannot be applied to operands of type string and Enum

  14. 14

    Why am I getting the error message "operator cannot be applied to operands of type string and int"?

  15. 15

    Operator '>=' cannot be applied to operands of type 'System.DateTime?' and 'int'

  16. 16

    Operator '-' cannot be applied to operands of type 'float' and 'string'

  17. 17

    operator <= cannot be applied to operands of type string and string

  18. 18

    Operator + cannot be applied to operands of type IntPtr and int - .Net 3.5

  19. 19

    Binary operator '|' cannot be applied to operands of type 'Int' and 'UInt8'

  20. 20

    Operator '==' cannot be applied to operands of type

  21. 21

    operator '||' cannot be applied to operands of type string and bool

  22. 22

    C# ERROR Operator * cannot be applied to operands of type 'string' and 'string'

  23. 23

    Operator '==' cannot be applied to operands of type string and char

  24. 24

    Error Message: Operator '+' cannot be applied to operands of type 'string' and 'method group'

  25. 25

    Operator '>' cannot be applied to operands of type 'object' and 'string'

  26. 26

    Error comparing Integers: Binary operator '<=' cannot be applied to operands of type 'Int?' and 'Int'

  27. 27

    Function error: "Binary operator '+' cannot be applied to operands of type 'Double' and 'Int'"

  28. 28

    Binary operator '..<' cannot be applied to operands of type 'Int' and 'Int?'

  29. 29

    Error : Binary operator '-=' cannot be applied to operands of type 'Int?' and 'Int'

HotTag

Archive