My (EF db first) type sits behind interface IPolicyNumber
. I get IQueryable<T>
and want to check what I got is correct type (does this table is searchable by that column which is determined by having that interface). Currently I am using typeof(IPolicyNumber).IsAssignableFrom(typeof(T))
which is a bit old school, I was wondering if there was a way to use something like:
IQueryable<T>.ElementType is IPolicyNumber
Full method is below:
public static IQueryable<T> ApplySearch<T>(this IQueryable<T> queryable, SearchModel search) where T : class
{
if (search != null && search.PolicyNumber.HasValue && typeof(IPolicyNumber).IsAssignableFrom(typeof(T)))
{
queryable = queryable.SearchByPolicyNumber(search);
}
return queryable;
}
public static IQueryable<IPolicyNumber> SearchByPolicyNumber<IPolicyNumber>(this IQueryable<IPolicyNumber> queryable, SearchModel search)
{
var policyNumberParameterLambda = Expression.Parameter((typeof(IPolicyNumber)));
var policyNumberColumnLambda = Expression.Property(policyNumberParameterLambda, "POLICY_NO");
var lambda = Expression.Lambda<Func<IPolicyNumber, bool>>(
Expression.Equal(policyNumberColumnLambda,
Expression.Convert(Expression.Constant(search.PolicyNumber), policyNumberColumnLambda.Type)
), policyNumberParameterLambda);
return queryable.Where(lambda);
}
Aucun commentaire:
Enregistrer un commentaire