Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write query where condition in List of shorts

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading