我创建了一个通用存储库类,如下所示:
public abstract class Repository<TEntity, TIdentifier> : IRepository<TEntity, TIdentifier> where TEntity : class
{
//TEntity is type of DBSet and TIdentifier is name of SQL Table's Id column
protected Repository(DbContext context)
{
Context = context;
DBSet = Context.Set<TEntity>();
}
}
在这个类中,我们有一个名为SelectAll
如下的方法:
[DataObjectMethod(DataObjectMethodType.Select, true)]
public IEnumerable<TEntity> SelectAll(IEnumerable<TIdentifier> ids, List<string> includeNavigationProperties = null)
{
ParameterExpression parameter = Expression.Parameter(typeof(TEntity), "entity");
MemberExpression leftParam = Expression.MakeMemberAccess(parameter, typeof(TEntity).GetProperty(IdentifierColumn));
Expression body = parameter;
foreach (TIdentifier identifier in ids)
{
var rightParam = Expression.Constant(identifier);
body = Expression.Or(body, Expression.Equal(leftParam, rightParam));//Exception
}
return (IEnumerable<TEntity>) DBSet.Select(Expression.Lambda<Func<TEntity, bool>>(body, parameter));
}
如您所见,我想选择所有带有 Id 列的行IEnumerable<TIdentifier> ids
。但是出现了以下异常:
二元运算符 Or 没有为类型“DAL.Tag”和“System.Boolean”定义