Illep
Illep

Reputation: 16851

LINQ filter fields that starts with a particular integer

I need to filter fields that starts with a particular integer or list of integers. For example, When I enter an integer (ex: 1), the query should return 1, 10, 119, 1187, 1098.

The following query gives me all the Schools that has an Id of 1. However, I need to modify it so it returns 1, 10, 119, 1187, 1098.

return await _dbContext.Schools.Where(c => c.Id == Convert.ToInt32(schoolId)).Take(5).ToListAsync() ;

Upvotes: 2

Views: 342

Answers (1)

Pratik Thanki
Pratik Thanki

Reputation: 252

You could do something like this, assuming:

  1. Id is an int
  2. schoolId is a string
return await _dbContext
   .Schools
   .Where(c => c.Id.ToString().StartsWith(schoolId)).Take(5).ToListAsync();

Upvotes: 1

Related Questions