将具有xml字符串属性的IQueryable源映射到IQueryable目标

时间:2018-12-12 07:44:42

标签: c# mapping automapper

继续, Map xml string property to C# properties

我有一个IQueryable Source,它需要与xml字符串属性的映射一起映射到IQueryable Destination类。 我尝试使用下面的资源,但是不起作用,

Mapper.Initialize(cfg =>
cfg.CreateMap<Source, Destination>()
.ConstructUsing(x => ConstructDestination(x)));


static Destination ConstructDestination(Source src)
{
       XDocument xdoc = new XDocument();
       xdoc = XDocument.Parse($"<Root>{src.Address}</Root>");

       return new Destination
       {
         Country = xdoc.Root.Element(nameof(Destination.Country)).Value,
         City = xdoc.Root.Element(nameof(Destination.City)).Value,
         Prefecture = xdoc.Root.Element(nameof(Destination.Prefecture)).Value,
       };

}

IQueryable<Destination> destQuery = srcQuery.ProjectTo<Destination>();
var result1 = destQuery.ToList(); 

0 个答案:

没有答案
相关问题