1

I have tried to below suggestions but nothing works, and I have edited the code to be easier. the purpose of this code is to get the row of the data based on the enum.

For example: if I give the value "bra" for search, then the wanted result should be brazil from enum but since the enum is an int then it's like I'm saying select the data where location = 3 if we suppose that brazil =3 in the enum.

public LocationType Location { get; set; }
    
public enum LocationType
{
    brazil,
    USA,
    UK
} 
public async Task<IActionResult> Index(string id, [FromForm] string search)

if (!string.IsNullOrWhiteSpace(search))
{
    result = applicationDbContext.Where(x => x.Location.ToString().Contains(search)).ToList();
}

note: search is string input.

  • I'm not sure, but maybe this can help you - [link](https://stackoverflow.com/questions/10709821/find-text-in-string-with-c-sharp) – ericmp Feb 01 '21 at 09:58
  • what do you mean by "doesn´t recognize it"? What output do you expect and what do you get instead? – MakePeaceGreatAgain Feb 01 '21 at 10:06
  • i think you missed, that an enum is a int type, you can use `Enum.GetName(typeof(LocationType), x.Location)` and comparing int casted to string compared with a char will end up in false. – TinoZ Feb 01 '21 at 10:11
  • how value of Location look like? – daremachine Feb 01 '21 at 10:11
  • `(char)locs` does definitly not what you expect it does. An enum is nothing but an int, and thus it can be directly converted to a char. **But** `(char)1` is not the letter `A`, but an ASCII-control-code. The letter `A` has the code 65, though. – MakePeaceGreatAgain Feb 01 '21 at 10:20

3 Answers3

1

Converting your Enum to string:

 Location.ToString();    //output = A

But This converts your Enum to a string containing all members:

string[] locs= Enum.GetNames(typeof(LocationType ));

which can be used in your query.

Rezaeimh7
  • 1,467
  • 2
  • 23
  • 40
1

If you want to get the numeric values (or enum values) of those that matches the search you can do it like below:

public async Task<IActionResult> Index(string id, [FromForm] string search)

if (!string.IsNullOrWhiteSpace(search))
{
     List<LocationType> locations = Enum.GetNames(typeof(LocationType))
               .Where(x => x.ToLower().Contains(search.ToLower()))
               .Select(x => Enum.Parse<LocationType>(x))
               .ToList();

     result = applicationDbContext.Where(x => locations.Contains(x.Location)).ToList();
}
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0

(char)locs does definitly not what you expect it does. An enum is nothing but an int, and thus it can be directly converted to a char. But (char)1 is not the letter A, but an ASCII-control-code. The letter A has the code 65, though.

So you have a couple of opportunities:

  1. make your enum start at 65:

    public enum LocationType
    {
        A = 65,
        B = 66,
        C = 67,
        K = 75
    }
    

    Now when you convert the enum to char you´d get A for example.

  2. Use enum.ToString(), e.g. LocationType.C.ToString() returns "C".

  3. The better idea however is to use the built-in functionality to get an enums name as m.r226 pointed out in his answer.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111