Casting error in DDS Linq Group by

Vote:
 
var query = store.Items<ProductImage>()
.GroupBy(image => new {MyBrand = image.Brand, MyProduct = image.Product})
.Select(grouped => new {MyBrand = grouped.Key.MyBrand, MyProduct = grouped.Key.MyProduct});

return query.ToList();

Invalid cast from 'System.Int32' to 'MyProject.Enums.Brand'.

 

on the line "return query.ToList();" I get that error. Would you know what's wrong with my query? Thanks.

 
#51555
Jun 15, 2011 8:18
Vote:
 

Hi Joe!

Yes, this is due to a bug in DDS in that doesnt handle Enum values correctyl if they are
selected in new anonymous types.

This can be worked around by casting the Enum temporarily into an Int and then perform
a postprocess step to convert it back into the Enum, something like below:

            var query = store.Items<ProductImage>()
                        .GroupBy(image => new { MyBrand = image.Brand, MyProduct = image.Product })
                        .Select(grouped => new { MyBrand = (int)grouped.Key.MyBrand, MyProduct = grouped.Key.MyProduct }); 
            
            
            var result = query.ToList()
                         .Select(a => new { MyBrand = (BrandEnum)a.MyBrand, MyProduct = a.MyProduct })
                         .ToList();

            return result;

    

Regards,
Johan

#51560
Jun 15, 2011 11:00
Vote:
 

Cool! Saves a lot of my energy. :)

Thank you very much.

#51576
Jun 16, 2011 4:32
Vote:
 

Hmm seems like a good solution, sadly our DDS Item contains an object which in turn contains quite many properties, some of them enum, quite alot to set and reset.

I solved this by creating an EnumTypeHandler. Blog post:

http://talk.alfnilsson.se/2011/06/27/using-propertydropdownlist-with-episerver-dynamic-properties/

#53535
Edited, Sep 13, 2011 16:25
Vote:
 

I have created a bug for this, with bug id: 74270.

This will be fixed to next version of the CMS.

#55010
Nov 15, 2011 14:41
Vote:
 

Awesome!

#55014
Nov 15, 2011 22:26
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.