1

When I trying to retrieve data from the database (SQL server/entity framework), I got below error:

The "==" operator cannot be used for operands of ("enumDropDown") or "int" operations.

and I can't write the query in the controller well, it throws me an error nearDropdownClassList == 2.

Controller: (i tried)


var listaOdbioruNadgodzin = _ecpContext.Karta.Where(x => x.Login == userName && x.DropdownClassList== 2)

Model:

public partial class modelCard
{
    public enumDropDown? DropdownClassList{ get; set; }
}

public enum enumDropDown
{
    test1 = 1,
    test2 = 2,
    test3 =3
}

Hamed Moghadasi
  • 1,523
  • 2
  • 16
  • 34
ZombieDivision
  • 183
  • 1
  • 11

3 Answers3

2

Change your code to this:

var listaOdbioruNadgodzin = _ecpContext.Karta.Where(
               x => x.Login == userName && 
               x.DropdownClassList== enumDropDown.test2)

You are currently comparing an enum value with a string "2" while you need to compare it with an enum type.

There are two more options for you (though not recommended and they perform idenitcally to each other How to compare enum and int values?)

x.DropdownClassList == (enumDropDown?)2

or

(int)x.DropdownClassList == 2
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
2

You should compare the property value to a value of the enum:

var listaOdbioruNadgodzin = _ecpContext.Karta.Where(x => x.Login == userName && x.DropdownClassList == enumDropDown.test2)
kaffekopp
  • 2,551
  • 6
  • 13
1

According to @juergen d's comment, you should change your code to compare enumDropDown instead of int.

var listaOdbioruNadgodzin = _ecpContext.Karta
             .Where(x => x.Login == userName && x.DropdownClassList == enumDropDown.test2);
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56