将值传递给列表 [英] passing value to list

查看:93
本文介绍了将值传递给列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法将类型为'System.Data.Objects.ObjectQuery`1 [<> f__AnonymousType 5`2 [System.String,System.String]]'的对象转换为'System' .Collections.Generic.List`





型号:



< pre lang =css> public class UserProductProperty
{

public global :System.String PName
{
get
;

set
;
}
public global :: System 字符串 PValue
{
get
;
set
;
}
}
public class UserWishListDetailModel
{
public 全局 :System.String ImagePath
{
获得
;

set
;
}
public global :: System 字符串 代码
{
获取
;
set
;
}

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







控制器:

 UserWishListDetailModel model =  new  UserWishListDetailModel(); 
var Wishproduct =( from p in db.Products 其中 p.ProductId == ProductId 选择 p)。();

model.ImagePath = Wishproduct.ImagePath;
model.Code = Wishproduct.Code;
model.ProductPropertyList =(List< UserProductProperty>)来自 prop db.ProductSelectedPropertyValues
其中 prop.ProductId == ProductId
选择
new
{
PName = prop.ProductPropertyValue.ProductProperty.Name,
PValue = prop.ProductPropertyValue.Value
};
return 查看(模型);

解决方案

你'将查询结果重新转换为 UserProductProperty 的列表,由于两个原因,这不会起作用;



1.查询结果是匿名类型;

选择这样;

 选择  new  
{
PName = prop.ProductPropertyValue.ProductProperty.Name,
PValue = prop.ProductPropertyValue.Value
};



不会创建 UserProductProperty 实例,但实例为一个新的匿名类型。



2.结果可能不是 List ,所有Linq都很有希望是 IEnumerable ,但你可以从 IEnumerable List c $ c>。



尝试更改作业到 model.ProductPropertyList 到此:

 model.ProductPropertyList =(来自 prop  db.ProductSelectedPropertyValues 
其中 prop.ProductId == ProductId
选择
new UserProductProperty
{
PName = prop.ProductPropertyValue.ProductProperty.Name,
PValue = prop.ProductPropertyValue.Value
})。ToList();



拨打 ToList 将创建一个 List 并选择 new UserProductProperty 将确保你得到你想要的类型。



希望这会有所帮助,

Fredrik


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 :

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 :

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

解决方案

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;

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:

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


这篇关于将值传递给列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆