C# 自动映射未映射列表<;int>;

C# 自动映射未映射列表<;int>;,c#,mapping,automapper,C#,Mapping,Automapper,我正在使用AutoMapper映射两个对象,但它不是int的映射列表 以下是我正在做的: Public class A { public int a{get;set;} public string b{get;set;} public List<int> ints{get;set;} } Public class B { public int a{get;set;} public string b{get;set;} public L

我正在使用AutoMapper映射两个对象,但它不是
int
的映射列表

以下是我正在做的:

Public class A
{
    public int a{get;set;}
    public string b{get;set;}
    public List<int> ints{get;set;}
}

Public class B
{
    public int a{get;set;}
    public string b{get;set;}
    public List<int> ints{get;set;}
}

public class MappingService
{

    public object MapBToA(B b)
   {
        Mapper.CreateMap<B,A>();
        return  Mapper.Map<B,A>(b);
   }
}
以上是完整的代码,当我调用ClassSearchRawInputToClassSearchInputmethod时,它将返回ClassSearchInputViewModel的me对象,但它返回的LocationID不包含项的0

下面是我的单元测试

    [Isolated]
    [TestMethod]
    public void ClassSearchRawInputToClassSearchInputViewModel_Test()
    {

        var urlProcessor = new SegmentToUrlSeoProcessingResultResolver();

        AutoMapperConfiguration.Configure();
        IMapperService mapper = new MapperService();
        int[] genericStringArray = {1, 2, 3};
        var searchRawInput = new SearchRawInput()
            {
                City = "Mumbai",
                Segment1 = "Dance",
                Segment2 = "Salsa",
                CategoryIds =  genericStringArray.ToList(),
                StartDate = DateTime.Now,
                EndDate = DateTime.Now,
                LocationGroupIds = genericStringArray.ToList(),
                LocationIds = genericStringArray.ToList(),
                StartPrice = genericStringArray[0],
                EndPrice = 2132123,
                SearchQuery = "Search Query",
                SubCategoryIds = genericStringArray.ToList(),
                TargetAge = TargetAgeGroup.Kids
            };

        Isolate.NonPublic.WhenCalled(urlProcessor, "ResolveCore", new SearchRawInput()
            {
                City = "Mumbai",
                Segment1 = "Dance",
                Segment2 = "Salsa",
                CategoryIds =  genericStringArray.ToList(),
                StartDate = DateTime.Now,
                EndDate = DateTime.Now,
                LocationGroupIds = genericStringArray.ToList(),
                LocationIds = genericStringArray.ToList(),
                StartPrice = genericStringArray[0],
                EndPrice = 2132123,
                SearchQuery = "Search Query",
                SubCategoryIds = genericStringArray.ToList(),
                TargetAge = TargetAgeGroup.Kids
            }).WillReturn(new UrlSeoProcessingResult()
                                                    {
                                                        LocationGroupId = 1,
                                                        SubCategoryId = 2,
                                                        IsError = false,
                                                        Type = SegmentType.GroupSubCategory
                                                    });
        var classSearchInput = mapper.ClassSearchRawInputToClassSearchInput(searchRawInput);
        Assert.AreEqual(TargetAgeGroup.Kids, classSearchInput.TargetAge, "Age is not mapped properly");
        Assert.AreEqual(3,classSearchInput.LocationIds.Count,"Location not mapped properly");
        Assert.AreEqual(3,classSearchInput.CategoryIds.Count,"Category is not mapped correctly");
    }

抱歉,我无法使用最新的AutoMapper NuGet(2.2.1)重现您描述的问题:

使用系统;
使用System.Collections.Generic;
使用自动制版机;
公共A类
{
公共int a{get;set;}
公共字符串b{get;set;}
公共列表整数{get;set;}
}
公共B级
{
公共int a{get;set;}
公共字符串b{get;set;}
公共列表整数{get;set;}
}
班级计划
{
静态void Main()
{
CreateMap();
var b=新的b
{
a=1,
b=“foo”,
ints=新列表(新[]{1,2,3,4})
};
var a=映射器.Map(b);
控制台写入线(a.ints.Count);
foreach(a.ints中的变量项)
{
控制台写入线(项目);
}
}
}

此控制台应用程序按预期在两种类型之间映射。所以我猜你还没有展示你真正的代码。你必须给出一个完整的例子来说明你的问题。

你是想调用Mapper.Map(b);是的,我写错了CreateMap,但它仍然不工作。为什么每次你想映射时都要调用Mapper.CreateMap?此方法应该在每个AppDomain中调用一次,理想情况下是在该AppDomain启动时调用。它仅在AppDomain中,这里我显示我已创建了一个map@rajansoft1:这些类都是
域模型
还是都是
视图模型
?我不知道;在任何
视图模型
中显示较少属性的任何类中,都看不到任何
反规范化
。这就是
自动映射器的主要用途
?是的,我没有分享我的完整代码,因为我不被允许分享公司政策,但我在a.intsWell中没有任何价值。那么也许你可以请你公司的政客们解决你的问题。在我的回答中,我已经向您发布了一个完整的工作示例。如果不允许您显示代码,请继续并尝试在示例应用程序中隔离问题,就像我在回答中所做的那样,然后在此处显示。如果你不告诉我们你在使用什么代码,我看不出你希望得到什么样的帮助。我已经添加了实际的代码,请帮助我解决这个问题。嘿,谢谢,问题现在解决了。这仅仅是因为我也为名单绘制了一张地图。
    [Isolated]
    [TestMethod]
    public void ClassSearchRawInputToClassSearchInputViewModel_Test()
    {

        var urlProcessor = new SegmentToUrlSeoProcessingResultResolver();

        AutoMapperConfiguration.Configure();
        IMapperService mapper = new MapperService();
        int[] genericStringArray = {1, 2, 3};
        var searchRawInput = new SearchRawInput()
            {
                City = "Mumbai",
                Segment1 = "Dance",
                Segment2 = "Salsa",
                CategoryIds =  genericStringArray.ToList(),
                StartDate = DateTime.Now,
                EndDate = DateTime.Now,
                LocationGroupIds = genericStringArray.ToList(),
                LocationIds = genericStringArray.ToList(),
                StartPrice = genericStringArray[0],
                EndPrice = 2132123,
                SearchQuery = "Search Query",
                SubCategoryIds = genericStringArray.ToList(),
                TargetAge = TargetAgeGroup.Kids
            };

        Isolate.NonPublic.WhenCalled(urlProcessor, "ResolveCore", new SearchRawInput()
            {
                City = "Mumbai",
                Segment1 = "Dance",
                Segment2 = "Salsa",
                CategoryIds =  genericStringArray.ToList(),
                StartDate = DateTime.Now,
                EndDate = DateTime.Now,
                LocationGroupIds = genericStringArray.ToList(),
                LocationIds = genericStringArray.ToList(),
                StartPrice = genericStringArray[0],
                EndPrice = 2132123,
                SearchQuery = "Search Query",
                SubCategoryIds = genericStringArray.ToList(),
                TargetAge = TargetAgeGroup.Kids
            }).WillReturn(new UrlSeoProcessingResult()
                                                    {
                                                        LocationGroupId = 1,
                                                        SubCategoryId = 2,
                                                        IsError = false,
                                                        Type = SegmentType.GroupSubCategory
                                                    });
        var classSearchInput = mapper.ClassSearchRawInputToClassSearchInput(searchRawInput);
        Assert.AreEqual(TargetAgeGroup.Kids, classSearchInput.TargetAge, "Age is not mapped properly");
        Assert.AreEqual(3,classSearchInput.LocationIds.Count,"Location not mapped properly");
        Assert.AreEqual(3,classSearchInput.CategoryIds.Count,"Category is not mapped correctly");
    }
using System;
using System.Collections.Generic;
using AutoMapper;

public class A
{
    public int a { get;set; }
    public string b { get;set; }
    public List<int> ints { get;set; }
}

public class B
{
    public int a { get;set; }
    public string b { get;set; }
    public List<int> ints { get;set; }
}

class Program
{
    static void Main()
    {
        Mapper.CreateMap<B, A>();
        var b = new B
        {
            a = 1,
            b = "foo",
            ints = new List<int>(new[] { 1, 2, 3, 4 })
        };

        var a = Mapper.Map<B, A>(b);
        Console.WriteLine(a.ints.Count);
        foreach (var item in a.ints)
        {
            Console.WriteLine(item);
        }
    }
}