I'm having trouble trying to implement OData with a custom DTO using Automapper's ProjectTo.
Lets say I have the following Objects:
public class Freight
{
public virtual ICollection<General.Fleet.Service> Services { get; set; }
}
public class FreightDTO
{
public virtual ICollection<DTO.General.Fleet.Service> Services { get; set; }
}
AutomapperConfiguration:
cfg.CreateMap<General.Fleet.Freight, DTO.General.Fleet.FreightDTO>()
.ForMember(x => x.Services, options => options.ExplicitExpansion())
cfg.CreateMap<DTO.General.Fleet.FreightDTO, General.Fleet.Freight>()
.ForMember(x => x.Services, options => options.ExplicitExpansion())
I'm trying to apply the OData filters through the ApplyTo method like so:
query.ApplyTo(db.Freight.ProjectTo<FreightDTO>()) as IQueryable<FreightDTO>;
But whenever I try to filter:
http://localhost:22564/api/Freights?$top=1&$filter=Services/any(s: contains(s/PrimaryCode, '41156') eq true)
I get the following error:
The specified type member 'Services' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
For what I understand I'm failling to filter by a property that is not included in the Projection. But I'm trying to avoid this include, because I don't want to bring all the Child objects, just filter the parent based on them.
What I don't want to do:
query.ApplyTo(db.Freight.ProjectTo<FreightDTO>(null, "Services")) as IQueryable<FreightDTO>;
Since I'm using OData's ODataQueryOptions 'ApplyTo' I'm not able to previously apply the filters in the dataset passed to Automapper (to filter directly on the entity rather than the DTO).
So my question is: Is there a way to filter a parent object based on the child, without having to bring the childs from the database?
Thanks in advance.
--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.