简体   繁体   English

Automapper-如何将两个不同的字符串映射到对象

[英]Automapper - how to map two different strings to object

var stringOne = "1";
var stringTwo = "2";

And model: 和型号:

public class TestModel
{
  public string TestStringOne { get; set;}
  public string TestStringTwo { get; set;}
}

How to to create maps and to map these two strings with Automapper? 如何使用Automapper创建地图并映射这两个字符串? I want to map stringOne to TestStringOne and stringTwo to TestStringTwo . 我想将stringOne映射到TestStringOne ,将stringTwo映射TestStringTwo

Are you trying to do this map as part of a larger mapping? 您是否要将此地图作为更大地图的一部分? Because as it is, it seems you should just do: 因为按原样,看来您应该这样做:

var foo = new TestModel() { TestStringOne = stringOne, TestStringTwo = stringTwo };

If you aren't trying to do a larger mapping, then the above is better than using AutoMapper. 如果您不打算做更大的映射,那么上面的内容比使用AutoMapper更好。 If you are trying to a larger mapping, then Automapper won't help you with these two, as it is meant to map one (not two or three) objects to another object. 如果您尝试使用更大的映射,则Automapper不会帮助您解决这两个问题,因为这意味着将一个(而不是两个或三个)对象映射到另一个对象。 If you really wanted to, you could lump all the objects together into a Tuple or a new type you write for this purpose, and then configure an AutoMapper mapping for that type, but I don't gather that's what you want. 如果确实需要,您可以将所有对象集中到为此目的编写的元组或新类型中,然后为该类型配置一个AutoMapper映射,但是我没有收集您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM