简体   繁体   English

如何使用从字符串到字符串列表的automapper

[英]How to use automapper from string to List of Strings

Using automapper how do we convert a string separated with spaces to a List ? 使用automapper我们如何将用空格分隔的字符串转换为List?

Data: 数据:

foo1 foo2 foo3 foo4

Class: 类:

public class myFooList
{
   public int myId;
   public List<string> myListOfStrings;
}

Using automapper defaults. 使用automapper默认值。

Mapper.CreateMap<data,myFooList>()        
    .ForMember(d=>d.mListOfStrings, s=>s.MapFrom(s=>s.Data));

I get data in the form of one line per character. 我以每个字符一行的形式获取数据。

Ex: 例如:

f
o
o
1

f
o
o
2

etc..etc.. etc..etc ..

It's pretty easy to split a string and create a list from it: 拆分字符串并从中创建列表非常容易:

var text = "foo1 foo2 foo3 foo4";
var delimiters = new char [] {' '};

var myListOfStrings = text.Split(delimiters).ToList();

I've never needed AutoMapper, so you may want to work from here... 我从来不需要AutoMapper,所以你可能想在这里工作......

Mapper.CreateMap<data,myFooList>()
.ForMember(d=>d.mListOfStrings, s=>s.MapFrom(s=>s.Data.Split()));

Looks like automapper makes a reasonable assumption and enumerates the string character by character. 看起来像automapper做出了一个合理的假设并按字符枚举字符串。 Just be explicit about the Split. 只是明确一下斯普利特。

nvoigt's suggestion is also correct - are you sure you want to use automapper? nvoigt的建议也是正确的 - 你确定要使用automapper吗?

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

相关问题 如何使用 AutoMapper 将 Child Object 列表转换为字符串列表或 Guid 列表 - How to use AutoMapper to convert Child Object list to either list of strings or list of Guids 如何使用 autoMapper 列出数据表? - How to use autoMapper for datatables to list? 如何使用自动映射器将枚举列表映射到字符串列表 - How to map enum list to string list with automapper 我如何从列表中获取 map<string> 列出<string>在 AutoMapper C# 中?</string></string> - How do I map from List<string> to List<string> in AutoMapper C#? 如何使用自动映射器将列表值映射到字符串? - How to Map List Values to String using Automapper? 如何使用Automapper展平实体层次结构列表? - How to use Automapper to flatten list of entity hierarchies? 如何从列表中删除特定的字符串<strings> - How to remove a specific string from list<strings> 如何使用 Linq 检查字符串列表是否包含列表中的任何字符串 - How to use Linq to check if a list of strings contains any string in a list Automapper:映射继承自 List 的两种类型<keyvaluepair<string, string> &gt;&gt; </keyvaluepair<string,> - Automapper : mapping two types inherited from List<KeyValuePair<string, string>>> 如何使用Automapper从DbGeometry转换为String? - How to convert from DbGeometry to String using Automapper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM