- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
LLBLGEN+AutoMapper+IQueryable: Unable to cast...
Joined: 06-Sep-2011
Hello,
I have read several threads in the forum about how to use AutoMapper with IQuerable, but I still cannot seem to resolve my issue. Basically, I have a business level repository that performs transformations on the data that is provided by a domain level repository. The domain level repository simply provides CRUD and list operations against the LLBLGEN DAL. The Business level respository decrypts/encrypts data and creates business entities from LLBLGEN domain entities. Our application is based on ASP.NET MVC4 + Razor and LLBLGEN (adapter mode, LLBLGEN framework).
So, when I try to do the following in the business repository:
var entities = AutoMapper.Mapper.Map<IQueryable<DE>, IQueryable<BE>>(DomainRepository.List);
I receive the following exception:
Unable to cast object of type 'System.Collections.Generic.List1[AbCd.Business.Entities.EntityName]' to type 'System.Linq.IQueryable
1[AbCd.Business.Entities.EntityName]'
I would like to be able to pass the Queryable along to my MVC controller, but I seem to be stuck... I read about LLBLGEN's QuerySpec, but I am not sure how to use it or how it will help with this error.
I'd appreciate your help and suggestions. I've included the business and domain code as well as the full exception.
Thank you for your time,
Mike
//////////////// BUSINESS REPOSITORY ///////////////////
// Performs transformations on the domain entities
// such as decryption/encryption and mapping
// domain entities <--> business entities
/// <summary>
/// Get a list of entities.
/// </summary>
/// <returns>A list of business entities or
/// null</returns>
//
// BE is an entity class that we created
// for our business logic. It contains
// the same data fields at the LLBLGEN
// domain entity.
public virtual IQueryable<BE> List
{
get
{
// DomainRepository is a repository
// that provides access to LLBLGEN
// entities, CRUD, list, and persistance.
var entities = AutoMapper.Mapper.Map
<
IQueryable<DE>,
IQueryable<BE>
>(DomainRepository.List);
return entities;
}
}
///////////////////////////////////////////////////////
////////////////// DOMAIN REPOSITORY ///////////////////
/// <summary>
/// Get a list of entities.
/// </summary>
/// <returns>A list of domain entities or
/// null</returns>
//
// DE is an entity class that was generated by LLBLGEN.
// DE means Domain Entity.
public virtual IQueryable<DE> List
{
get
{
LinqMetaData linq = new LinqMetaData(Adapter);
if (linq == null)
{
throw new NullReferenceException("linq");
}
// Get the datasource for the type.
// Replaces something like linq.Customers in a normal query.
var dataSource =
GetQueryableForEntity(linq, typeof(DE)) as DataSource2<DE>;
return dataSource;
}
}
///////////////////////////////////////////////////////////
/////////////////// EXCEPTION DETAILS //////////////////
System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Unable to cast object of type 'System.Collections.Generic.List`1[AbCd.Business.Entities.EntityName]' to type 'System.Linq.IQueryable`1[AbCd.Business.Entities.EntityName]'.
Source=AutoMapper
StackTrace:
at AutoMapper.MappingEngine.Map[TSource,TDestination](TSource source)
at AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
at AbCd.Business.Repositories.BusinessRepository`4.get_List() in c:\PROJECTS\AbCdMVC\AbCd.Business\Repositories\BusinessRepository.cs:line 203
at AbCd.WebUI.Controllers.CustomerController.Index() in c:\PROJECTS\AbCdMVC\AbCd.WebUI\Controllers\CustomerController.cs:line 100
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
InnerException:
////////////////////////////////////////////////////////
I think that's the same issue as here: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=17364
Joined: 06-Sep-2011
Hi,
Actually, I read that posting and tried its solution, but it didn't correct things. I understand that AutoMapper needs to have the list exist and not be a query, so I've converted the mapping to use an IEnumerable for the output, and I return it to my application. IEnumerable will work fine.
Thank you for your response and suggestion,
Mike