Case sensitive fields

Posts   
 
    
gemfore
User
Posts: 35
Joined: 30-Jul-2010
# Posted on: 19-Aug-2010 00:10:15   

Hi I have the following code

Public Function GetFieldWrapper(ByVal entity As EntityClasses.CommonEntityBase, ByVal fieldName As String) As EntityField2 Return CType(entity.Fields(fieldName), EntityField2) End Function

This works fine however the fieldname appears to be case sensiitive.

Is there a way around this i.e. so it works even if the cases dont match?

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Aug-2010 07:03:17   

You should create a dictionary containing the fieldnames lowercased as KEY and the corresponding field as the VALUE. Then you can access the dictionary with you lowercased string parameter.

David Elizondo | LLBLGen Support Team
gemfore
User
Posts: 35
Joined: 30-Jul-2010
# Posted on: 19-Aug-2010 19:41:15   

Thanks for that however i'm sorry but i dont fully understand?

Do you have any working examples demonstrating this?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 19-Aug-2010 20:47:35   

Somwhere in the entity initialisation routine - sorry C#, I don't do VB past version 6!


Dictionary <string,EntityField2> lowercaseFieldNames = new Dictionary <string,EntityField2>();
foreach (EntityField2 field in entity.Fields)
{
    lowercaseFieldNames.Add(field.Name.ToLower(),field);
}


In your function


    return lowercaseFieldNames[fieldname.ToLower()];

If using .Net 3.5 just do it with LINQ (so you don't need the dictionary)

In your function


   return entity.Fields.Single(x=>x.Name.ToLower() == fieldName.ToLower());