从Automapper映射IQueryable类

时间:2014-06-16 13:37:49

标签: automapper

Automapper是否适用于IQueryable?

我有2个查询

IQueryable<V_ImageUpload> Query1;
IQueryable<V_ImageUpload> Query2;

Mapper.CreateMap<IQueryable<V_ImageUpload_WithReceiptBackup>, IQueryable<V_ImageUpload>>();
Query1 = Mapper.Map<IQueryable<V_ImageUpload_WithReceiptBackup>, IQueryable<V_ImageUpload>>(Query2);

发生异常是:

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary

1 个答案:

答案 0 :(得分:3)

我没有使用过AutoMapper(所以这是尝试它的借口!),但有queryable extensions可用。你的两个查询都属于同一类型,所以我不确定你想要实现什么,但也许这样你想要的东西,需要IQueryable<V_ImageUpload_WithReceiptBackup>并将其转换为{{1} }:

IQueryable<V_ImageUpload>

IQueryable<V_ImageUpload_WithReceiptBackup> query1; IQueryable<V_ImageUpload> query2; // Only map the actual type, not the queryable types Mapper.CreateMap<V_ImageUpload_WithReceiptBackup, V_ImageUpload>(); query2 = query1.Project().To<V_ImageUpload>(); 将其保留为.Project().To<V_ImageUpload>(),而IQueryable最终会以Mapper.Map结尾。我只使用LINQ to Objects测试了这一点,但希望它可以与Entity Framework一起使用,或者无论你使用什么。