0

私は2つのクラスを持っています:

source.Employee と destination.Employee 。

関数の宛先プロパティ名、つまりdestination.TestEnum1とソースプロパティ名、つまりsource.TestEnum1に2つの名前しかありません。

以下で述べたように、式を動的に作成したいと思います。

 var mapExpr = Mapper.CreateMap<Soure.Employee, destination.Employee>().ForMember(destination => destination.TestEnum1, opt => opt.MapFrom(source => (destination.MyEnum2)source.TestEnum1));

表現はあくまで

destination => destination.TestEnum1, opt => opt.MapFrom(source => (destination.MyEnum2)source.TestEnum1)

Project().To(); で Enum をマップするために作成しています。として

Mapper.CreateMap<Soure.MyEnum1, destination.MyEnum2>() 

MyEnum2 を int 32 にマップできないという例外が発生します。

元の従業員:

namespace Soure
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Department dept1 { get; set; }

        public int age { get; set; }

        public MyEnum1 TestEnum1 { get; set; }
    }

    public enum MyEnum1
    {
        red = 1,
        yellow = 2
    }
}

宛先の従業員クラス:

namespace destination
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int age { get; set; }
        public MyEnum2 TestEnum1 { get; set; }

        public Departments dept1 { get; set; }

    }
    public enum MyEnum2
    {
        red = 1,
        yellow = 2
    }
}
4

3 に答える 3

0

enumsあなたの例では同じ基本的な型を共有しているため、intオートマッパーはこれを次のように自動的に処理します。

    Mapper.CreateMap<Foo, Bar>()
    .ForMember(dest => dest.MyEnum2, opt => opt.MapFrom(src => src.MyEnum1))    
    ;

    var source = new Foo { MyEnum1 = MyEnum1.yellow };

    var destination = Mapper.Map<Bar>(source);

    Console.WriteLine(destination.MyEnum2);

働くフィドル

于 2015-01-12T12:59:45.640 に答える
0

Custom Type Convertersこれを使用することを考えるかもしれません。ここにあります

Enum1to2TypeConverterここで、ジェネリック インターフェイスからの継承を実装したサンプル アプリケーションを作成しました (少し簡略化しました) ITypeConverter

public class Employee1
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyEnum1 TestEnum { get; set; }
}

public enum MyEnum1
{
    red = 1,
    yellow = 2
}

public class Employee2
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyEnum2 TestEnum { get; set; }
}

public enum MyEnum2
{
    red = 1,
    yellow = 2
}

public class Enum1to2TypeConverter : ITypeConverter<MyEnum1, MyEnum2>
{
    public MyEnum2 Convert(ResolutionContext context)
    {
        return (MyEnum2)(context.SourceValue);
    }
}

public class Test
{
    public void Example()
    {
        Mapper.CreateMap<MyEnum1, MyEnum2>().ConvertUsing(new Enum1to2TypeConverter());
        Mapper.CreateMap<Employee1, Employee2>();
        Mapper.AssertConfigurationIsValid();

        var source = new Employee1
        {
            Id = 1,
            Name = "Employee1-Name",
            TestEnum = MyEnum1.yellow
        };

        Employee2 result = Mapper.Map<Employee1, Employee2>(source);
        //Check content of result here!
    }
}

class Program
{
    private static void Main(string[] args)
    {
        (new Test()).Example();
    }
}
于 2015-01-12T07:58:27.230 に答える
0

LINQ の ConvertUsing である ConvertProjectionUsing を確認できます。

Mapper.CreateMap<MyEnum1, MyEnum2>().ProjectUsing(src => ???);

その中に何を入れる必要がありますか???、よくわかりません。クエリプロバイダーによって異なります。EF は、そのプロジェクションを取得して SQL に変換できる場合とできない場合があります。ソースを int にキャストできる場合があります。

Mapper.CreateMap<MyEnum1, MyEnum2>().ProjectUsing(src => (MyEnum2)(int)src);

ただし、これはクエリ プロバイダーに完全に依存します。

于 2015-01-13T01:10:40.823 に答える