NHibernate ExcludeProperty:为什么没有IncludeProperty

NHibernate ExcludeProperty:为什么没有IncludeProperty,nhibernate,Nhibernate,如果要指定要从示例中排除哪些属性,那么示例类非常有用。但是,如果要指定要包括哪些属性,该怎么办 举个例子:在数据库中查找同名的人。 Person对象具有许多属性。因此,要使用NHibernate.criteria.Example对象,我必须指定要排除的每个字段——可能有很多字段 为什么没有IncludeProperty方法 我有一个Person对象,我想根据预先设置的业务规则(FirstName、LastName、DateOfBirth)查看它是否是一个双面对象。这些规则可以更改为包含邮政编码或

如果要指定要从示例中排除哪些属性,那么示例类非常有用。但是,如果要指定要包括哪些属性,该怎么办

举个例子:在数据库中查找同名的人。 Person对象具有许多属性。因此,要使用NHibernate.criteria.Example对象,我必须指定要排除的每个字段——可能有很多字段

为什么没有IncludeProperty方法

我有一个Person对象,我想根据预先设置的业务规则(FirstName、LastName、DateOfBirth)查看它是否是一个双面对象。这些规则可以更改为包含邮政编码或其他内容,我希望可以对其进行配置


有没有简单的解决方法?

我有一个IncludeProperty问题的解决方案:

private Type persitentType = typeof(T);
public IList<T> GetByExample(T exampleInstance, params string[] propertiesToInclude)
{
    // get the properties that will be excluded
    List<string> propertiesToExclude =
        persitentType.GetProperties().Where(p => propertiesToInclude.Contains(p.Name) == false).Select(p => p.Name).ToList();

    // create the criteria based on the example and excluding the given properties
    ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
    Example example = Example.Create(exampleInstance);
    foreach (string propertyToExclude in propertiesToExclude)
    {
        example.ExcludeProperty(propertyToExclude);
    }
    criteria.Add(example);

    // return the result 
    return criteria.List<T>();
}
private Type persitentType=typeof(T);
公共IList GetByExample(T exampleInstance,参数字符串[]propertiesToInclude)
{
//获取将被排除的属性
列出属性排除=
persitentType.GetProperties().Where(p=>propertiesToInclude.Contains(p.Name)==false)。选择(p=>p.Name.ToList();
//根据示例创建标准,并排除给定的属性
ICriteria-criteria=NHibernateSession.CreateCriteria(persitentType);
Example=Example.Create(exampleInstance);
foreach(propertiesToExclude中的字符串propertyToExclude)
{
示例.ExcludeProperty(propertyToExclude);
}
标准。添加(示例);
//返回结果
返回条件。List();
}

将此方法添加到存储库类。它使用反射确定指定对象具有哪些属性,然后根据已指定为包含的属性查找要排除的属性。

我有一个IncludeProperty问题的解决方案:

private Type persitentType = typeof(T);
public IList<T> GetByExample(T exampleInstance, params string[] propertiesToInclude)
{
    // get the properties that will be excluded
    List<string> propertiesToExclude =
        persitentType.GetProperties().Where(p => propertiesToInclude.Contains(p.Name) == false).Select(p => p.Name).ToList();

    // create the criteria based on the example and excluding the given properties
    ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
    Example example = Example.Create(exampleInstance);
    foreach (string propertyToExclude in propertiesToExclude)
    {
        example.ExcludeProperty(propertyToExclude);
    }
    criteria.Add(example);

    // return the result 
    return criteria.List<T>();
}
private Type persitentType=typeof(T);
公共IList GetByExample(T exampleInstance,参数字符串[]propertiesToInclude)
{
//获取将被排除的属性
列出属性排除=
persitentType.GetProperties().Where(p=>propertiesToInclude.Contains(p.Name)==false)。选择(p=>p.Name.ToList();
//根据示例创建标准,并排除给定的属性
ICriteria-criteria=NHibernateSession.CreateCriteria(persitentType);
Example=Example.Create(exampleInstance);
foreach(propertiesToExclude中的字符串propertyToExclude)
{
示例.ExcludeProperty(propertyToExclude);
}
标准。添加(示例);
//返回结果
返回条件。List();
}

将此方法添加到存储库类。它使用反射确定指定对象具有哪些属性,然后根据已指定为包含的属性查找要排除的属性。

为什么不能排除SELECT中的列?我的代码中不需要任何SQL。通过包含SQL,我将自己束缚在一种特定的数据库技术上。我只想使用对象。为什么不能在SELECT中排除列?我的代码中不需要任何SQL。通过包含SQL,我将自己束缚在一种特定的数据库技术上。我只想使用这些对象。