I have to write a query using C# / LINQ, the equivalent of this:
select RoleName from dbo.Roles where RoleId in (2,3,4,5,6)
In my code I have a list of short ids (idRoles is a list of shorts ( example: 1,2,3,4)) and this code:
List<string> roleNameList = new List<string>();
roleNameList = _dbContext
.Roles
.Where(r => idRoles.Contains(r.RoleId))
.Select(r => r.RoleName);
gives an error:
Cannot convert from System.Linq.IQueryable ‘<‘string’>’ to string.
But in my table Role the IdRole is a short(smallint)
>Solution :
try this
List<string> roleNameList = _dbContext
.Roles
.Where(r => idRoles.Contains(r.RoleId))
.Select(r => r.RoleName)
.ToList();