Try our conversational search powered by Generative AI!

Casting error in DDS Linq Group by

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.

Jun 15, 2011 8:18

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 })

            return result;



Jun 15, 2011 11:00

Cool! Saves a lot of my energy. :)

Thank you very much.

Jun 16, 2011 4:32

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:

Edited, Sep 13, 2011 16:25

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

This will be fixed to next version of the CMS.

Nov 15, 2011 14:41


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.