How to search string in Enums and return the matching result?

saurav.rox

New member
Joined
Apr 13, 2023
Messages
3
Programming Experience
1-3
I have an entity called Account which contains a column called AccountType which is an enum with multiple enum items such as Principal, Student, Staff, etc. So, I am trying to search for a string that matches any of these enums and return the list of matching Accounts.

Here is how I am trying:
C#:
var accQueryList = _context.Accounts.Where(x => true);
accQueryList = Enum.GetNames(typeof(AccountTypeEnum)).Contains(parameters.Filter.ToString().ToLower()); //Error is seen here
var items = _mapper.Map<List<AccountVM>>(await accQueryList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync());

However, the second line throws an error saying: Cannot implicitly convert type 'bool' to 'System.Linq.IQueryable'

AccountType Enum is declared as:
C#:
namespace Services.Common.Enums.Account
{
    public enum AccountTypeEnum
    {
       Principal, Student, Staff
    }
}

Any help on how to search for matching strings in Enum and return the result list? Please let me know for any further clarification.

Thank you,

1681973096975.png
 
Last edited by a moderator:
For future reference, the Inline Code button is for formatting words or expressions within a sentence, e.g. including a type name like string. If you're posting a block of code, use the Code button instead. I have fixed your post for you this time.
 
Moving to Entity Framework...
 
I have an entity called Account which contains a column called AccountType which is an enum with multiple enum items such as Principal, Student, Staff, etc. So, I am trying to search for a string that matches any of these enums and return the list of matching Accounts.
You need to better explain how you've wrired that up. Show a screenshot of your DB query tool after it has selected some rows from this table. Show the code in your context config and any custom converters you may have written for this column

Here is how I am trying:
C#:
var accQueryList = _context.Accounts.Where(x => true);
accQueryList = Enum.GetNames(typeof(AccountTypeEnum)).Contains(parameters.Filter.ToString().ToLower()); //Error is seen here
var items = _mapper.Map<List<AccountVM>>(await accQueryList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync());

However, the second line throws an error saying: Cannot implicitly convert type 'bool' to 'System.Linq.IQueryable'
Of course. You did this:

C#:
var accQueryList = _context.Accounts.Where(x => true);
So `accQueryList` is an IQueryable of accounts

And then you did this

C#:
accQueryList = Enum.GetNames(typeof(AccountTypeEnum)).Contains(parameters.Filter.ToString().ToLower());
This isnt invoking any EF database calls or anything like that, it's just running Enum.GetNames, which gets a string array of the names in some enum and then asking the string array if it contains some string. It's nothing to do with EF, or a database, and it results in a `bool`, which you're trying to assign to a variable that can only store an IQueryable

You'd get the same with this code:

C#:
var x = context.Accounts.Where(a => a.Limit == 100);
x = true;


If you want an IQueryable, you can use call ToQueryable; you dont have to invoke a useless always-true Where clause
 

Latest posts

Back
Top Bottom