如果您正在寻找一个可以用来附加公司 ID 的函数,如果实体上存在公司 ID,请检查您的表达式,这应该可以解决问题:
public static Expression<Func<T, bool>> CheckPropertyIfExists<T, TProperty>(Expression<Func<T, bool>> expression, string propertyName, TProperty propertyValue)
{
Type type = typeof(T);
var property = type.GetProperty(propertyName, typeof(TProperty));
if(property == null || !property.CanRead)
return expression;
return expression.Update(
Expression.And( // &&
Expression.Equal( // ==
Expression.MakeMemberAccess(expression.Parameters[0], property), // T parameter.{propertyName}
Expression.Constant(propertyValue) // specified propertyValue constant
),
expression.Body // Original expression
),
expression.Parameters
);
}
你可以这样使用它:
public T First<T>(Expression<Func<T, bool>> expression, int companyId)
{
var set = GetObjectSet<T>();
return set.FirstOrDefault<T>(CheckPropertyIfExists(expression, "CompanyId", companyId));
}
现在您可以使用您的表达式和您想要过滤的 companyId 调用您的 First 方法。
更好的方法可能是将其用作过滤方法,即将其重写为不需要内部表达式并适用于对象查询(或 IQueryable)的扩展方法:
public static ObjectQuery<T> FilterByPropertyIfExists<T, TProperty>(this ObjectQuery<T> query, string propertyName, TProperty propertyValue)
{
Type type = typeof(T);
var property = type.GetProperty(propertyName, typeof(TProperty));
if(property == null || !property.CanRead)
return query;
var parameter = Expression.Parameter(typeof(T), "x");
Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)Expression.Lambda(
Expression.Equal( // ==
Expression.MakeMemberAccess(parameter, property), // T parameter.{propertyName}
Expression.Constant(propertyValue) // specified propertyValue constant
),
parameter
);
return query.Where(predicate);
}
它的美妙之处在于它可以很好地与标准 LINQ 语法(查询和流利)一起工作。
例如它允许这样的查询:
from x in repository.Clients.FilterByPropertyIfExists("Company", 5)
where x == ???
select x.Name;
[编辑]
我已经对其进行了一些清理,并添加了对参数可见性(必须是公共的静态属性)以及 ObjectQuery 实现(将自动用于 ObjectQuery 和 ObjectSet)的检查:
public static class QueryExtensions
{
public static IQueryable<T> FilterByPropertyIfExists<T, TProperty>(this IQueryable<T> query, string propertyName, TProperty propertyValue)
{
Type type = typeof(T);
var property = type.GetProperty(
propertyName,
BindingFlags.Instance | BindingFlags.Public, // Must be a public instance property
null,
typeof(TProperty), // Must be of the correct return type
Type.EmptyTypes, // Can't have parameters
null
);
if (property == null || !property.CanRead) // Must exist and be readable
return query; // Return query unchanged
// Create a predicate to pass to the Where method
var parameter = Expression.Parameter(typeof(T), "it");
Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)Expression.Lambda(
Expression.Equal( // ==
Expression.MakeMemberAccess(parameter, property), // T parameter.{propertyName}
Expression.Constant(propertyValue) // specified propertyValue constant
),
parameter
);
return query.Where(predicate); // Filter the query
}
public static ObjectQuery<T> FilterByPropertyIfExists<T, TProperty>(this ObjectQuery<T> query, string propertyName, TProperty propertyValue)
{
var filteredQuery = FilterByPropertyIfExists((IQueryable<T>)query, propertyName, propertyValue);
return (ObjectQuery<T>)filteredQuery; // Cast filtered query back to an ObjectQuery
}
}