简体   繁体   English

C#Lambda表达式映射多个条件

[英]C# Lambda Expression Mapping Multiple Conditions

I am using Enterprise Library.I want to map the column (of integer type) to Enum Type. 我正在使用Enterprise Library。我想将列(整数类型)映射到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. 我将Database Table的列映射到C#Types(Class Employee)属性。

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扩展方法中映射Enum的多个条件。我的意思是

.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? 帮我查一下多种情况?

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 . 三元运算符

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 . 或者,您可以创建一个函数并在Func使用它。

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. 如果您熟悉lambda强制转换和调用,它也更容易阅读。 Abstracting this lambda into a general int-to-Enum function would be optimal. 将此lambda抽象为一般的int-to-Enum函数将是最佳的。

.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")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM