实体检索方法应检索具有取消属性等于false的实体

时间:2014-05-20 14:40:31

标签: c# properties repository

所有实体都派生自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应该检索它,否则它应该为空。

对于Entity2IList<Entity3>子项应仅包含Cancelled设置为false的实体。

我正在尝试使用反射来确定实体的属性是否具有等于BaseEntity

的基本类型
if (property.PropertyType.BaseType == typeof(BaseEntity))

但如何将其添加到查询中?

0 个答案:

没有答案