我正在使用ExpressMapper将Linq-To-Sql对象映射到另一个对象 - 但是我的字符串中的空值导致了问题。
有没有办法从ExpressMapper中将这些空值转换为string.Empty
?
E.g。鉴于以下类别:
class A
{
string a = null;
}
class B
{
string a;
}
进行转换时
B b = Mapper.Map<A, B>(new A(), new B());
我想b.a == ""
而不是b.a == null
答案 0 :(得分:1)
您可以使用helloworld.c
功能:
xb@dnxb:/tmp/c c$ gdb -q /tmp/c\ c/main
expansion: History expansion on command input is on.
filename: The filename in which to record the command history is "/home/xiaobai/.gdb_history".
remove-duplicates: The number of history entries to look back at for duplicates is 0.
save: Saving of the history record on exit is on.
size: The size of the command history is 10000000.
Reading symbols from /tmp/c c/main...done.
(gdb) my
Breakpoint 1 at 0x40063a: file main.c, line 5.
Breakpoint 1, main () at main.c:5
5 hello();
(gdb) sn
[customStepping]
******************** /usr/src/glibc/glibc-2.24/sysdeps/unix/sysv/linux/_exit.c /tmp/c c/main.c ********************
hello () at helloworld.c:3
3 printf("Hello world!\n");
(gdb)
[customStepping]
******************** /tmp/c c/helloworld.c ********************
File #1 [Stepping] Disabled
Hello world!
4 printf("Next line\n");
(gdb)
[customStepping]
******************** /tmp/c c/helloworld.c ********************
File #1 [Stepping] Disabled
Next line
5 }
(gdb)
[customStepping]
******************** /tmp/c c/helloworld.c ********************
File #1 [Stepping] Disabled
main () at main.c:6
6 return 0;
(gdb)
[customStepping]
******************** /tmp/c c/main.c ********************
7 }
(gdb)
[customStepping]
******************** /tmp/c c/main.c ********************
__libc_start_main (main=0x400636 <main>, argc=1, argv=0x7fffffffd468, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>,
stack_end=0x7fffffffd458) at ../csu/libc-start.c:325
325 exit (result);
(gdb)
...
答案 1 :(得分:1)
您可以使用null-coalescing operator。
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
通过这种方式,您可以简化对此的调用:
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.