[英]ExpressMapper - null to string.Empty
I'm using ExpressMapper to map a Linq-To-Sql object to another object - but null values in my strings are causing issues. 我正在使用ExpressMapper将Linq-To-Sql对象映射到另一个对象-但是字符串中的null值会引起问题。 Is there any way to convert these null values to
string.Empty
from within ExpressMapper? 有没有办法从ExpressMapper内部将这些空值转换为
string.Empty
?
Eg Given the following classes: 例如,考虑以下类别:
class A
{
string a = null;
}
class B
{
string a;
}
When doing the conversion 进行转换时
B b = Mapper.Map<A, B>(new A(), new B());
I want to get ba == ""
not ba == null
我想得到
ba == ""
不是ba == null
您可以使用Member
功能:
Mapper.Register<A,B>().Member(dest => dest.a, src => src.a == null ? string.Empty : src.a);
You can use the null-coalescing operator . 您可以使用null-coalescing运算符 。
string a = "";
string b = null;
string c = a ?? "xyz"; //a is not null, so empty string is assigned to c
string d = b ?? "xyz"; //b is null, so "xyz" is assigned to d
This way you can simplify your call to this: 这样,您可以简化对此的调用:
B b = Mapper.Map<A, B>(new A(), new B());
b.a = b.a ?? "";
//"" can be String.Empty, whichever you prefer for your code style.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.