You could use an Expression Tree to dynamically build a Linq where clause to filter on dynamic property.
I know this is probably a lot to digest but, here goes. Replace StockItem with the type of the StockView DbSet
[HttpGet("{searchText}/{filterType}")]
public async Task<ActionResult<List<Stock>>> Get(string searchText, string filterType)
{
var queryableStockView = this._context.StockView;
// w =>
var param = Expression.Parameter(typeof(StockItem), "w");
var propertyInfo = typeof(StockItem).GetProperty(filterType);
if (propertyInfo == null)
throw new Exception($@"Property ""{property}"" was not found");
// w.[filterType]
var left = Expression.Property(param, propertyInfo);
// searchText
var right = Expression.Constant(searchText, typeof(string));
// w.[filterType] == searchText
var expression = Expression.Equal(left, right);
// Bring it all together
// Where(w => (w.[filterType] == searchText))
var whereExpression = Expression.Call(
typeof(Queryable),
nameof(System.Linq.Enumerable.Where),
new Type[] { queryableStockView.ElementType },
queryableStockView.Expression,
Expression.Lambda<Func<StockItem, bool>>(expression, new ParameterExpression[] { param })
);
// Run query against the database
var filteredItems = queryableStockView.Provider.CreateQuery<StockItem>(whereExpression);
var v = await filteredItems.ToListAsync();
return this.Ok(v);
}
The dynamically generated Linq Expression should get translated to SQL without any issues.