C# 为什么我的空替换在automapper中不起作用
我已经用c#编写了一个程序,并使用automapper进行映射。我使用了Null替换,但它没有替换结果中的给定值。我只得到Null。请检查下面的代码。我希望我的结果显示809900078C# 为什么我的空替换在automapper中不起作用,c#,automapper,C#,Automapper,我已经用c#编写了一个程序,并使用automapper进行映射。我使用了Null替换,但它没有替换结果中的给定值。我只得到Null。请检查下面的代码。我希望我的结果显示809900078 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Automapper; namespace Automapper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automapper;
namespace Automapper
{
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Mobile { get; set; }
}
public class Patient
{
public string Name { get; set; }
public string Location { get; set; }
public int age { get; set; }
public string phone { get; set; }
static void Main(string[] args)
{
User obj = new User();
obj.FirstName = "sujit";
obj.LastName = "kumar";
obj.Address = "Bangalore";
obj.Mobile = "";
AutoMapper.Mapper.CreateMap<User, Patient>().ForMember(
emp => emp.Name, map => map.MapFrom(p => p.FirstName + " " + p.LastName))
.ForMember(dest => dest.Location, source => source.MapFrom(x => x.Address))
.ForMember(dest => dest.phone, source => source.NullSubstitute("8099000078"));
var result = AutoMapper.Mapper.Map<User, Patient>(obj);
// Console.WriteLine(result.Name);
// Console.WriteLine(result.Location);
Console.WriteLine(result.phone);
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用自动制版机;
名称空间自动映射器
{
公共类用户
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串地址{get;set;}
公共字符串Mobile{get;set;}
}
公立病人
{
公共字符串名称{get;set;}
公共字符串位置{get;set;}
公共整数{get;set;}
公用字符串电话{get;set;}
静态void Main(字符串[]参数)
{
User obj=新用户();
obj.FirstName=“sujit”;
obj.LastName=“kumar”;
对象地址=“班加罗尔”;
obj.Mobile=“”;
AutoMapper.Mapper.CreateMap().FormMember(
emp=>emp.Name,map=>map.MapFrom(p=>p.FirstName+“”+p.LastName))
.ForMember(dest=>dest.Location,source=>source.MapFrom(x=>x.Address))
.ForMember(dest=>dest.phone,source=>source.NullSubstitute(“809900078”);
var结果=AutoMapper.Mapper.Map(obj);
//Console.WriteLine(result.Name);
//控制台写入线(结果位置);
控制台。写入线(结果。电话);
Console.ReadLine();
}
}
}
您错过了null替换的工作原理
发件人:
空替换允许您为
目标成员,如果源值沿
成员链
在您的代码中,您没有为phone
属性提供任何源代码,因此它没有检查null的任何值,也不执行null替换
我对您的代码进行了一些更改以使其正常工作(请参见下文):
void Main()
{
User obj=新用户();
obj.FirstName=“sujit”;
obj.LastName=“kumar”;
对象地址=“班加罗尔”;
obj.Mobile=null;
var config=new-MapperConfiguration(cfg=>cfg.CreateMap()
.ForMember(emp=>emp.Name,map=>map.MapFrom(p=>p.FirstName+“”+p.LastName))
.ForMember(dest=>dest.Location,source=>source.MapFrom(x=>x.Address))
.ForMember(dest=>dest.phone,source=>source.MapFrom(x=>x.Mobile))
.ForMember(dest=>dest.phone,source=>source.NullSubstitute(“809900078”));
var mapper=config.CreateMapper();
var result=mapper.Map(obj);
result.Dump();
}
公共类用户
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串地址{get;set;}
公共字符串Mobile{get;set;}
}
公立病人
{
公共字符串名称{get;set;}
公共字符串位置{get;set;}
公共整数{get;set;}
公用字符串电话{get;set;}
}
这里我为
phone
属性提供了一个源代码(我从Mobile
映射了它),并设置了null
值。我以前没有使用过Automapper,但是从语法上看,“source”似乎永远不会为null,因为您向它传递了一个有效的obj(因此永远不会让NullSubstitute()返回809900078)
我看不出您试图在哪里映射dest=>dest.phone,source=>source.Mobile。这应该可以-
.ForMember(dest => dest.phone, source => source.MapFrom(a => !string.IsNullOrWhiteSpace(a.Mobile) ? a.Mobile : "8099000078"));
对于初学者,我不会使用名称空间
Automapper
。很容易出现打字错误,使您无法使用正确的名称空间(就像您在使用块中所做的那样)。您好,它工作得很好,但我希望它可以与NullSubstitute一起使用,您能为此提供任何解决方案吗?我一定会研究它
.ForMember(dest => dest.phone, source => source.MapFrom(a => !string.IsNullOrWhiteSpace(a.Mobile) ? a.Mobile : "8099000078"));