ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

WhereStringIsNotNullOrEmpty

Where string is not null or empty extension method for NHibernate 3.0 and its new query API QueryOver.

Source

public static IQueryOver<TRoot, TSubType> WhereStringIsNotNullOrEmpty<TRoot, TSubType>(this IQueryOver<TRoot, TSubType> query, Expression<Func<TRoot, string>> expression)
{
	return WhereStringIsNotNullOrEmpty(query, expression.Body);            
}

public static IQueryOver<TRoot, TSubType> WhereStringIsNotNullOrEmpty<TRoot, TSubType>(this IQueryOver<TRoot, TSubType> query, Expression<Func<string>> expression)
{
	return WhereStringIsNotNullOrEmpty(query, expression.Body);            
}

private static IQueryOver<TRoot, TSubType> WhereStringIsNotNullOrEmpty<TRoot, TSubType>(IQueryOver<TRoot, TSubType> query, System.Linq.Expressions.Expression expression)
{
	var projectionInfo = ExpressionProcessor.FindMemberProjection(expression);
	var property = projectionInfo.AsProjection();

	var criteria = Restrictions.Or(Restrictions.IsNull(property), Restrictions.Eq(Projections.SqlFunction("trim", NHibernateUtil.String, property), ""));
	return query.Where(Restrictions.Not(criteria));
}

Example

public class TeamEmployeeRepository : Repository<TeamEmployee>, ITeamEmployeeRepository
{
    public IEnumerable<TeamEmployee> GetEmployees()
    {
        return GetSession().QueryOver<TeamEmployee>()
						   .WhereStringIsNotNullOrEmpty(p => p.Name)
					       .Future();
    }
}

Or by alias...

public class TeamEmployeeRepository : Repository<TeamEmployee>, ITeamEmployeeRepository
{
    public IEnumerable<TeamEmployee> GetEmployees()
    {
		TeamEmployee TeamEmployeeAlias = null; 
        return GetSession().QueryOver<TeamEmployee>(() => TeamEmployeeAlias)
						   .WhereStringIsNotNullOrEmpty(() => TeamEmployeeAlias.Name)
					       .Future();
    }
}

Author: Richard

Submitted on: 25 jun 2016

Language: C#

Type: WhereStringIsNotNullOrEmpty

Views: 3209