C# Lambda 表达式映射多个条件
我正在使用企业库。我想将列(整数类型)映射到枚举类型。
假设
Enum BloodGroup Type
{
OPositive,
ONegative,
ABPositive,
ABNegative,
BPositive,
BNegative,
NotSet
}
我将数据库表的列映射到 C# 类型的(员工类)属性。
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();
代码工作正常,但我想在 WithFun
扩展方法中映射枚举的多个条件。我的意思是
.WithFun(rec=> rec.IsDBNull(rec.GetOrdinal("BloodGroup")) ? BloodGroup.NotSet
rec.GetOrdinal("BloodGroup")==1 ?BloodGroup.OPositive
rec.GetOrdinal("BloodGroup")==2 ?BloodGroup.ONegative
)
帮助我检查多个条件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要添加的只是一些冒号和最后的 else 表达式。请参阅三元运算符。
All you need to add are some colons and a final else expression. See ternary operator.
use
基本上你可以使用花括号来定义你的函数。或者,您可以创建一个函数并在
Func
中使用它。use
Basically you can use curly braces to define your function. Alternatively you can create a function and use it in
Func
.尝试这样的事情。使用此代码,您可以创建一个只需接触记录一次的匿名函数,从而提高了整体效率。如果您熟悉 lambda 转换和调用,它也更容易阅读。将此 lambda 抽象为通用的 int-to-Enum 函数将是最佳选择。
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.