无法在复杂对象列表中找到对象(使用自动映射器)

Cannot find an object inside a list of complex object (using automapper)

第一次使用automapper,我怀疑这是罪魁祸首。我的搜索页面创建一个搜索对象,然后将其传递给业务 class。如果我什么都不搜索,或者搜索一个名字,我会得到大约 40000 条正确的记录。如果我搜索排名,我会收到以下错误。

任何帮助将不胜感激

附加信息:未将对象引用设置为对象的实例。

   at lambda_method(Closure , Casualty )
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.<SkipIterator>d__4d`1.MoveNext()
   at System.Linq.Enumerable.<TakeIterator>d__3a`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Business.Casualty.Search.Execute(Casualty Search, Int32 startRowIndex, Int32 maxRows, Int32& TotalCount) in c:\projects\casulaties\Business\Entities\casualty.cs:line 198
   at UnitTest.TestAutoMapper.Casualty.TestMethod1() in c:\projects\casulaties\UnitTest\TestAutoMapper\Casualty.cs:line 22

商业

private static List<Casualty> _casualties = null;
    private static List<Casualty> Casualties
    {
        get
        {
            if (_casualties == null)
            {
                List<Repository.Casualty> casualtiesRepo;
                using (casualtiesEntities ctx = Utility.getEntity())
                {                       
                   casualtiesRepo= ctx.Casualties
                                .Include(x => x.Cemetery)
                                .Include(x => x.Country)
                                .Include(x => x.Rank)
                                .Include(x => x.Locality)
                                .Include(x => x.Trade)
                                .Include(x => x.Regiment)
                                .Include(x => x.Unit1)
                                .Include(x => x.Unit2)
                                .Include(x => x.Volunteers)
                                .Include(x => x.Photos)                                             
                                .OrderBy(x => x.LastName).ThenBy(x => x.FirstName).ToList();
                  _casualties = Mapper.Map<List<Casualty>>(casualtiesRepo);  
                }                                        
            }

            return _casualties;
        }
    }

            public static List<Casualty> Temp(Casualty Search)
        {

            List<Business.Casualty> Repo;
            IQueryable<Business.Casualty> y = Casualties.AsQueryable();

            //Name
            if (Search.Name != null)
            {
                if (!string.IsNullOrWhiteSpace(Search.Name.first))
                {
                    y = y.Where(x => x.Name.first.ToLower().Contains(Search.Name.first.ToLower()) ||
                                        x.Name.first.ToLower().Contains(Search.Name.first.ToLower()));
                }
                if (!string.IsNullOrWhiteSpace(Search.Name.last))
                {
                    y = y.Where(x => x.Name.last.ToLower().Contains(Search.Name.last.ToLower()) ||
                                        x.Name.last.ToLower().Contains(Search.Name.last.ToLower()));
                }
                //Initials
                if (!string.IsNullOrWhiteSpace(Search.Name.initials))
                {
                    Search.Name.initials = Business.Common.TextHelper.UnSlugify(Search.Name.initials);
                    y = y.Where(x => x.Name.initials.ToLower().Contains(Search.Name.initials.ToLower()));
                }
            }
            //RankID
            if (Search.Rank != null)
            {
                if (Search.Rank.ID > 0)
                {
                    y = y.Where(x => x.Rank.ID == Search.Rank.ID);
                }
            }

            Repo = y.ToList();  **<---SEARCH BOMBS HERE**

            return Repo.ToList();

        }

AutoMapper

 ////Casualty
        Mapper.CreateMap<Business.Casualty, Repository.Casualty>()
            .ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.Name.first))
            .ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.Name.last))
            .ForMember(dest => dest.Initials, opt => opt.MapFrom(src => src.Name.initials))
            .ForMember(dest => dest.Cemetery, opt => opt.MapFrom(src => new Cemetery() { ID = src.Cemetery.ID, Name = src.Cemetery.Name, Latitude = src.Cemetery.Latitude, Longitude = src.Cemetery.Longitude, }))
            .ForMember(dest => dest.Country, opt => opt.MapFrom(src => new Country() { ID = src.Country.ID, Name = src.Country.Name }))
            .ForMember(dest => dest.Rank, opt => opt.MapFrom(src => new Rank() { ID = src.Rank.ID, Name = src.Rank.Name }))
            .ForMember(dest => dest.Locality, opt => opt.MapFrom(src => new Locality() { ID = src.Locality.ID, Name = src.Locality.Name }))
            .ForMember(dest => dest.Trade, opt => opt.MapFrom(src => new Trade() { ID = src.Trade.ID, Name = src.Trade.Name }))
            .ForMember(dest => dest.Regiment, opt => opt.MapFrom(src => new Regiment() { ID = src.Regiment.ID, Name = src.Regiment.Name }))
            .ForMember(dest => dest.Unit1, opt => opt.MapFrom(src => new Unit1() { ID = src.Unit1.ID, Name = src.Unit1.Name }))
            .ForMember(dest => dest.Unit2, opt => opt.MapFrom(src => new Unit2() { ID = src.Unit2.ID, Name = src.Unit2.Name }))
            .ForMember(dest => dest.Photos, opt => opt.MapFrom(src => new List<Photo>()))
            .ForMember(dest => dest.Volunteers, opt => opt.MapFrom(src => new List<Volunteer>()));

        Mapper.CreateMap<Repository.Casualty, Casualty>()
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => new Name() { first = src.FirstName, last = src.LastName, initials = src.Initials }))             
            .ForMember(dest => dest.Country, opt => opt.MapFrom(src => new Country() { ID = src.Country.ID, Name = src.Country.Name }))
            .ForMember(dest => dest.Rank, opt => opt.MapFrom(src => new Rank() { ID = src.Rank.ID, Name = src.Rank.Name }))
            .ForMember(dest => dest.Locality, opt => opt.MapFrom(src => new Locality() { ID = src.Locality.ID, Name = src.Locality.Name }))
            .ForMember(dest => dest.Trade, opt => opt.MapFrom(src => new Trade() { ID = src.Trade.ID, Name = src.Trade.Name }))
            .ForMember(dest => dest.Regiment, opt => opt.MapFrom(src => new Regiment() { ID = src.Regiment.ID, Name = src.Regiment.Name }))
            .ForMember(dest => dest.Unit1, opt => opt.MapFrom(src => new Unit1() { ID = src.Unit1.ID, Name = src.Unit1.Name }))
            .ForMember(dest => dest.Unit2, opt => opt.MapFrom(src => new Unit2() { ID = src.Unit2.ID, Name = src.Unit2.Name }))
            .ForMember(dest => dest.Photos, opt => opt.MapFrom(src => new List<Photo>()))
            .ForMember(dest => dest.Volunteer, opt => opt.MapFrom(src => new List<Volunteer>()))                
            ;

单元测试

       Business.Casualty Search = new Business.Casualty();
        Search.Rank = new Business.Rank { ID = 429};
        //Search.Name = new Business.Name { first = "joe" };


       List<Business.Casualty> _MainList = new List<Business.Casualty>();
        _MainList = Business.Casualty.Search.Temp(Search);

我想问题出在你的 linq,而不是自动映射器:

在您的 linq 中,您假设每个对象都有一个等级 属性 - 是这样吗?

  if (Search.Rank.ID > 0)
                {
                    y = y.Where(x => x.Rank.ID == Search.Rank.ID);
                }

如果没有,我会把它改成

  if (Search.Rank.ID > 0)
                {
                    y = y.Where(x => x.Rank != null && x.Rank.ID == Search.Rank.ID);
                }