第一次使用automapper,我怀疑这是罪魁祸首。 “我的搜索”页面创建一个Search对象,然后将其传递给Business类。如果我什么都没搜索,或者搜索一个名字,我会得到正确的40000左右的记录。如果我搜索Rank,我会收到以下错误。
任何帮助都将受到高度赞赏
附加信息:未将对象引用设置为对象的实例。
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);
答案 0 :(得分:1)
我想问题是你的linq,而不是自动映射器:
在你的linq中你假设每个对象都有一个Rank属性 - 就是这种情况吗?
String text = "Nm n n 4 n n bkj nun4hmnun Onn njnb hm5bn nm55m nbbh n mnrrnut but n rym4n nbn 4nn65 m nun m n nn nun 4nm 5 gm n my b bb b b rtmrt55tmmm5tttn b b bb g bn nn n h r ret n nun bn d. B bbbbbbbbbbr bung NHnhn nn nk, v v v n gain t g 4gnyhimmigration ndn nb NVnb bin uny 7 nbbbbbnn vv bbvb ninn njnj n4 nm n km n n n cb j bun. Nhfnt bn nn. N hm nn nun m bum my b mmmnbjk nn n by nn nun nun n nun nn bn n nhn n nn n n m NH nb4mnm mkn 4 n n n n hm r b rnfngg4d in b nut mmmkmmm5 bbjn n n ij BBM 8u8i by nun n.nn hm n. n4n By 4n4n bunny RN bny hm j mi. Nymmn FBT not mn n n nm g by n n nnm? Rnyb vCard n5 Yu nn n n n n nt .nm mn nt n nb n n n n by y5nnnhyyh h b b nt njj n m f4n re";
Properties props = new Properties();
props.setProperty("annotators","tokenize, ssplit, pos,parse,sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation annotation = pipeline.process(text);
如果没有,我会将其更改为
if (Search.Rank.ID > 0)
{
y = y.Where(x => x.Rank.ID == Search.Rank.ID);
}