KenAdams
KenAdams

Reputation: 21

Cannot convert between Data entity and Domain entity IQueryable with Automapper for OData endpoint

I am trying to convert my data entity IQueryable to domain entity IQueryable in my repository class. The Find() method is used by OData to apply filters. The problem is, when I send an OData query without any filters, it works fine because no filter is applied, but when I try to send a filter in the query, I get an error about conversion. Here is my method

public IQueryable<TDomain> Find()
{
    var database = Client.GetDatabase(DatabaseName);
    var collection = database.GetCollection<TEntity>(CollectionName);

    AggregateOptions aggregateOptions = new AggregateOptions()
    {
        Collation = new Collation("en", strength: CollationStrength.Secondary)
    };

    var domainEntities = collection.AsQueryable(aggregateOptions:     aggregateOptions).ProjectTo<TDomain>(mapper.ConfigurationProvider);

    return domainEntites;
}

and this is the error I get

"Object of type 'MongoDB.Driver.Linq.Linq3Implementation.MongoQuery2[Infrastructure.DataEntities.MarketEntity,Domain.Entities.Market]' cannot be converted to type 'System.Linq.IQueryable1[Infrastructure.DataEntities.MarketEntity]'.",

Can someone please explain why this isn't working? I have tried different approaches of mapping between the two but none of them is working. My main objective is to keep the method generic and not use .ToList(), I need to work with IQueryable, because I have huge collections in my MongoDB.

I have a map created between the two in my Mapping Profile class and that mapping is working fine in all the other methods. It's the IQueryable that is causing the issue.

Please let me know what other information I can provide to make this easier to understand. Thanks a lot in advance.

Upvotes: 2

Views: 36

Answers (0)

Related Questions