Click here to Skip to main content
15,915,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[<>f__AnonymousType 5`2[System.String,System.String]]' to type 'System.Collections.Generic.List`


model :

CSS
public class UserProductProperty
   {

       public global::System.String PName
       {
           get
         ;
           set
          ;
       }
       public global::System.String PValue
       {
           get
          ;
           set
           ;
       }
   }
   public class UserWishListDetailModel
   {
       public global::System.String ImagePath
       {
           get
         ;
           set
          ;
       }
       public global::System.String Code
       {
           get
         ;
           set
          ;
       }

       public List<UserProductProperty> ProductPropertyList { get; set; }
   }



controller :
C#
UserWishListDetailModel model = new UserWishListDetailModel();
            var Wishproduct = (from p in db.Products where p.ProductId == ProductId select p).Single();

            model.ImagePath = Wishproduct.ImagePath;
            model.Code = Wishproduct.Code;
            model.ProductPropertyList = (List<UserProductProperty>)from prop in db.ProductSelectedPropertyValues
                                                                   where prop.ProductId == ProductId
                                                                   select
                                                                  new
                                                                  {
                                                                      PName = prop.ProductPropertyValue.ProductProperty.Name,
                                                                      PValue = prop.ProductPropertyValue.Value
                                                                  };
            return View(model);
Posted

1 solution

You're casting the result of the query to a list of UserProductPropertys, and that's not going to work because of two reasons;

1. The results of the query are of an anonymous type;
Selecting like this;
C#
select new
{
  PName = prop.ProductPropertyValue.ProductProperty.Name,
  PValue = prop.ProductPropertyValue.Value
};

Will not create a UserProductProperty instance, but an instance of a new anonymous type.

2. The result might not be a List at all, all Linq is promising is that it is IEnumerable, but you can create a List from the IEnumerable.

Try changing you assignment to model.ProductPropertyList to this:
C#
model.ProductPropertyList = (from prop in db.ProductSelectedPropertyValues
                             where prop.ProductId == ProductId
                             select
                             new UserProductProperty
                             {
                               PName = prop.ProductPropertyValue.ProductProperty.Name,
                               PValue = prop.ProductPropertyValue.Value
                             }).ToList();

The call to ToList will create a List and selecting new UserProductProperty will make sure you get the type you want.

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900