tags:

views:

58

answers:

3

I am using Enterprise Library.I want to map the column (of integer type) to Enum Type.

Say

Enum BloodGroup Type
{

  OPositive,
  ONegative,
  ABPositive,
  ABNegative,  
  BPositive,
  BNegative,
  NotSet 
} 

I am mapping Database Table's column to C# Types's (Class Employee) Properties.

IRowMapper<Employee> addressMapper = MapBuilder<Employee>
                .MapAllProperties()   // map all properties
                .Map(p=>p.BloodGroup)  // override BloodGroup property
                .WithFunc(rec => rec.IsDBNull(rec.GetOrdinal("BloodGroup"))
                ? BloodGroup.NotSet
                : BloodGroup.OPositive)
                .Build();

Code working fine but i want to map multiple condition of Enum in WithFun extension Method.I mean something like

.WithFun(rec=> rec.IsDBNull(rec.GetOrdinal("BloodGroup")) ?   BloodGroup.NotSet
               rec.GetOrdinal("BloodGroup")==1 ?BloodGroup.OPositive
               rec.GetOrdinal("BloodGroup")==2 ?BloodGroup.ONegative
         )

Help me to check multiple condition?

+1  A: 

use

 .WithFunc((rec) => {
 if(rec.IsDBNull(rec.GetOrdinal("BloodGroup"))
    return BloodGroup.NotSet;
 else if(rec.GetOrdinal("BloodGroup")==1)
   return BloodGroup.OPositive;
 else if(rec.GetOrdinal("BloodGroup")==2)
    return BloodGroup.ONegative;
   ..... // you get the idea        
 })
 . // rest

Basically you can use curly braces to define your function. Alternatively you can create a function and use it in Func.

Aliostad
Thank You very much
IExtensible
+2  A: 
rec.IsDBNull(rec.GetOrdinal("BloodGroup")) ? BloodGroup.NotSet :
rec.GetOrdinal("BloodGroup")==1 ? BloodGroup.OPositive :
rec.GetOrdinal("BloodGroup")==2 ? BloodGroup.ONegative :
BloodGroup.NotSet

All you need to add are some colons and a final else expression. See ternary operator.

David B
Yes It is working fine
IExtensible
+1  A: 

Try something like this. Using this code, you create an anonymous function that only has to touch the record once, increasing all-around efficiency. It's also easier to read, if you're familiar with lambda casting and invoking. Abstracting this lambda into a general int-to-Enum function would be optimal.

.WithFun(rec => ((Func<BloodGroup, int>)
    (i => 
        { 
            if(rec.IsDBNull(i)) return BloodGroup.NotSet;
            switch(i) 
            { 
                case 1: 
                    return BloodGroup.OPositive;
                case 2: 
                    return BloodGroup.ONegative;
                // More cases...
                default:
                    return BloodGroup.NotSet;
            }
        })).Invoke(rec.GetOrdinal("BloodGroup")))
Legatou