1

I Have Two Tables: Properties,Rentals

Which have couple similar properties like:AreaInSquareMeters,RentalPrice, etc.. I have DTO to which I want to project them and then union them in a whole. But if I map the collections to the DTO then exception raises:

The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument.Parameter name: argument

That's a little code which raise the exception

var properties = Context.Properties.Select(property => new PropertyInfoDTO
            {
                Id = property.Id,
                PropertyName = property.PropertyName,
                SellingPrice = property.SellingPrice,
                RentalPrice = property.RentalPrice, 
                Images = property.Images.Select(image => new PropertyImagesInfoDTO
                {
                    ImagePath = image.ImagePath,
                    ImageRatio = image.ImageRatio
                }).ToList(), 
                other properties mapped here
           }

Then:

var rentals = Context.Properties.Select(property => new PropertyInfoDTO
            {
                Id = property.Id,
                PropertyName = property.Property.PropertyName,
                SellingPrice = property.Property.SellingPrice,
                RentalPrice = property.RentalPrice, 
                    Images = property.Property.Images.Select(image => new PropertyImagesInfoDTO
                    {
                        ImagePath = image.ImagePath,
                        ImageRatio = image.ImageRatio
                    }).ToList(), 
                other properties mapped here
           }

and I want to make the as a whole:

properties = properties.Union(rentals);

Everything is ok till I make a query to the DB and try to materialize the query Any suggestions of how to Cast both to Some DTO and Aggregate them ?

1
  • Did you mean var rentals = Context.Rentals?
    – Travis J
    Oct 4, 2018 at 20:31

1 Answer 1

1

Union is an implicit Distinct. There's no trivial way to determine distinct objects when the objects have collection members. That's what the exception is trying to say.

But, seeing your PropertyInfoDTO class, you probably didn't intend to make the collection distinct anyway. The structure of PropertyInfoDTO demands that you use Concat, which simply combines two collections, without implicit Distinct.

However, in EF6, if you do...

properties = properties.Concat(rentals);

...you'll get an EntityCommandCompilationException saying

The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'

...which means that EF doesn't succeed in translating the query expression into one SQL statement. In EF-core you'll also get an exception, but a different one.

There's no other way to work around these exceptions than forcing the two queries to run separately:

var properties = Context.Properties.Select(...)
    .AsEnumerable();
properties = properties.Concat(rentals);
5
  • That's very bad limitation especially in my case, because i wanted to filter that IQueryable afterwards, which is not a big deal ( I can apply filters to the queries as they're separate and then materialize them), but the real issue arises when i want to order them by any of the properties and page them(say in pages of six). And the problem here is that i order the Properties and the Rentals, and after i merge them they are not ordered and i have to order them again then it will be correct Collection on which to apply paging. Oct 5, 2018 at 6:27
  • I understand that i can skip the IQueryable ordering, but i cant take advance of Skip and Take. I have to materialize all rentals and properties every time and apply all actions. Oct 5, 2018 at 6:27
  • The other way around would be to create some kind of cache, preferably on the client side which will make it run smooth changing the pages, but handling the browser back button will be pain in the neck Oct 5, 2018 at 6:30
  • Yeah, that's bad. Could you get the flat objects first, as anonymous types, by one query with ordering and paging (server-side) and then their images in a separate query? Oct 5, 2018 at 6:59
  • Perfect. I have a couple other collections in the DTO, but as i think of your suggestion it will be best if i stick to it. Well each collection will be fetched in separate query but at least i'll the treat the Properties and the Rentals as a whole. Other way around would be to change the db structure to something like Composite pattern ( merge rentals and properties and some discriminator ). I'll approach it as you say, thanks for the help. Oct 5, 2018 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.