Then track performance of query you can invoke ToQueryString method. If you have a lot of parameters in query it just add @p1, @p2, etc. What to do if you want inline it?
You can just use this expression visitor for inline parameters:
public class ParameterReplacer : System.Linq.Expressions.ExpressionVisitor
{
protected override Expression VisitMember(MemberExpression node)
{
if (node.Expression is ConstantExpression constantExpression)
{
var container = constantExpression.Value;
var member = node.Member;
if (member is System.Reflection.FieldInfo field)
{
var value = field.GetValue(container);
if (value is string or not IEnumerable)
{
return Expression.Constant(value, field.FieldType);
}
}
else if (member is System.Reflection.PropertyInfo property)
{
var value = property.GetValue(container);
if (value is string or not IEnumerable)
{
return Expression.Constant(value, property.PropertyType);
}
}
}
return base.VisitMember(node);
}
public static IQueryable<T> ReplaceParamToConst<T>(IQueryable<T> query)
{
var replacer = new ParameterReplacer();
return query.Provider.CreateQuery<T>(replacer.Visit(query.Expression));
}
}
Usage: ParameterReplacer.ReplaceParamToConst(query).ToQueryString();