0

I have a string that is being passed to my controller in Razor Pages. This string contains the name of one of 8 possible Booleans as selected by the user. I am attempting to use this Boolean Name in a SQL server query as shown below.

try
{
     var test = await _context.GuestModel
     .Include(r => r.PatientModel)
     .OrderByDescending(r => r.GuestLastName)
     .Where(r => r.booleanName  == true)
     .ToListAsync();
     return test;
}

booleanName is the string containing the Boolean Name I need to use, but so far I have not been able to access that information successfully.

  • You can't just do that in a straightforward manner. You'll either have to build up the expression tree yourself using reflection, or use a library such as [DynamicLinq](https://dynamic-linq.net/) to do that for you. – Kirk Woll Jun 07 '22 at 14:02
  • 1
    At some point, you are going to have to translate what the Razor page calls the property and what the database calls the field. They shouldn't (have to) be the same. – Neil Jun 07 '22 at 14:02

1 Answers1

0

Use a switch or if/else blocks to pick from one of eight possible versions of the query based on the string.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794