ProjectTo ExplicitExpansion Filter child EF

511 views
Skip to first unread message

quierou...@gmail.com

unread,
Oct 11, 2017, 4:38:54 PM10/11/17
to AutoMapper-users

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.

Jimmy Bogard

unread,
Oct 11, 2017, 4:59:51 PM10/11/17
to automapper-users

--
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.

Pablo Rodriguez

unread,
Oct 11, 2017, 5:20:27 PM10/11/17
to AutoMapper-users
Thanks for the quick reply! Could you give me a little example on how to use it to solve this particular problem? When using UseAsDataSource the IDE hints that is obsolete.

Thanks again!

Thomas Tomiczek

unread,
Oct 31, 2017, 5:19:35 AM10/31/17
to AutoMapper-users
Same problem here.

According to the Automapper Unit Tests this is not really supported yet:

https://github.com/AutoMapper/AutoMapper/blob/master/src/AutoMapperSamples.OData/ODataQueryTests.cs Test named SupportsExpand() is marked as "[Ignore("Not Supported yet")].
Reply all
Reply to author
Forward
0 new messages