所有实体都派生自BaseEntity
,其Cancelled
属性类型为bool
。我正在修改使用
GetList
方法
public static IList<T> GetList<T>(Expression<Func<T, bool>> predicate) where T : BaseEntity
谓词作为方法参数(其中T
派生自BaseEntity
)。我知道可以创建一个新方法GetListNonCancelled
并向现有查询添加Where
子句,如
query.Where(e => e.Cancelled == false)
将返回Cancelled
属性设置为false
的实体列表,但是如果实体具有属性,该属性也是从BaseEntity
派生的实体或派生的实体数组从BaseEntity
开始,如何在搜索查询中添加where子句以指定它还应仅检索Cancelled
属性设置为false
的实体?
实施例
public class BaseEntity
{
public bool Cancelled { get; set; }
public int Id { get; set; }
}
public class Entity1 : BaseEntity
{
public string Name { get; set; }
public Entity3 Child { get; set; }
}
public class Entity2 : BaseEntity
{
public string Name { get; set; }
public IList<Entity3> Children { get; set; }
}
public class Entity3 : BaseEntity
{
public string Name { get; set; }
}
因此,当我想要检索Entity1
类型的实体列表时,我应该只获得将Cancelled
属性设置为false
的实体,以及属性Child
实体将Cancelled
设置为false
应该检索它,否则它应该为空。
对于Entity2
,IList<Entity3>
子项应仅包含Cancelled
设置为false
的实体。
我正在尝试使用反射来确定实体的属性是否具有等于BaseEntity
if (property.PropertyType.BaseType == typeof(BaseEntity))
但如何将其添加到查询中?