upper() with dbFunctionCall

Posts   
 
    
magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 24-Nov-2008 20:09:57   

Hi,

I am trying use an iPredicate to compare a value to a column in a case-independent way. I got this idea from another thread here in the forum:


        public static IPredicate iCaseEquals(EntityField2 field, string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                value = value.ToUpper();
            }

            IExpression upperFunc = new DbFunctionCall("UPPER", new object[] { field });
            EntityField upperField = new EntityField("upper", upperFunc);
            IPredicate predicate = (upperField == value);

            return predicate;
        }

Everything compiles fine, but then I run into a NullReferenceException ("Object reference not set to an instance of an object.") when using fetching the entity collection in here:


// create a filter for the unique look up constraint
IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(iCaseEquals(CityFields.Name, name));
EntityCollection<CityEntity> results = new EntityCollection<CityEntity>();
adapter.FetchEntityCollection(results, filter);

I would be more than happy for some hints.

I am working with LGP 2.6, MSSQL 2005, and .NET.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Nov-2008 03:26:11   

Now there's an easy way:

private IPredicate iCaseEquals(EntityField2 field, string value)
{
    if (!string.IsNullOrEmpty(value))
    {
        value = value.ToUpper();
    }
    
    FieldCompareValuePredicate predicate = new FieldCompareValuePredicate(field, null, ComparisonOperator.Equal, value);
    predicate.CaseSensitiveCollation = true;

    return predicate;
}
David Elizondo | LLBLGen Support Team
magic
User
Posts: 125
Joined: 24-Nov-2008
# Posted on: 26-Nov-2008 17:38:03   

work's perfect. Thank you! simple_smile