如何使用Automapper将列表值映射到字符串?

时间:2016-08-05 12:11:30

标签: c# automapper

这是我的Dto模型对象

String editValueName= editName.getText().toString();
String lastStatus = valueStatus.getText().toString();
newFragment .setText(editValueName, lastStatus);

我正在映射到我的ViewModel

public class SourceClass
{
    public List<SourceList> SourceLists { get; set; }
}

public class SourceList
{
    public bool Type1 { get; set; }
    public bool Type2 { get; set; }
    public bool Type3 { get; set; }
    public decimal Value { get; set; }
}

使用我自己的解析器方法。 目标是如果Dto List具有相应的bool为true,则将值映射到List to ViewModel值。

例如, 如果列表包含IsValue1为true,则需要使用来自IsValue1的值更新viewModel Value1。

它当前正在更新viewModel中具有相同值的所有属性的值,即Value1,Value2和Value3具有相同的value1值。当它查找条件IsValue1时,除非我删除该值,否则在dto中为真。

public class DestinationClass
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }

}

代码的实现是这样的:

public class Resolver : ValueResolver<SourceClass, string>
{
    protected override string ResolveCore(SourceClass source)
    {
        if (source.SourceLists.Any())
        {
            foreach (var sourceList in source.SourceLists)
            {
                //if the source list Type1 is true, add the value of the list to the value1
                if (sourceList.Type1.Equals(true))
                {
                    var value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    return value1;
                }

                if (sourceList.Type2.Equals(true))
                {
                    var value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    return value1;
                }

                if (sourceList.Type3.Equals(true))
                {
                    var value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    return value1;
                }
            }
        }
        return String.Empty;
    }
}

我可以更改代码并使用单独的映射器来纠正这一点,但这反过来会破坏使用Automapper的目的。

1 个答案:

答案 0 :(得分:0)

我解决这个问题的方法是每次满足条件时删除值。我使用了RemoveAt函数来完成它。

我的解决方案

  public class Resolver : ValueResolver<SourceClass, string>
   {
    protected override string ResolveCore(SourceClass source)
    {
        var dc = new DestinationClass();
        if (source.SourceLists.Any())
        {
            foreach (var sourceList in source.SourceLists)
            {
                //if the source list Type1 is true, add the value of the list to the value1
                if (sourceList.Type1.Equals(true))
                {
                    dc.Value1 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    source.SourceLists.RemoveAt(0);
                    return dc.Value1;
                }

                if (sourceList.Type2.Equals(true))
                {
                    dc.Value2 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    source.SourceLists.RemoveAt(0);
                    return dc.Value2;
                }

                if (sourceList.Type3.Equals(true))
                {
                    dc.Value3 = sourceList.Value.ToString(CultureInfo.InvariantCulture);
                    source.SourceLists.RemoveAt(0);
                    return dc.Value3;
                }
            }
        }
        return String.Empty;
    }
}