将值传递给列表 [英] passing value to list
问题描述
无法将类型为'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 ofUserProductProperty
s, 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 aUserProductProperty
instance, but an instance of a new anonymous type.
2. The result might not be aList
at all, all Linq is promising is that it isIEnumerable
, but you can create aList
from theIEnumerable
.
Try changing you assignment tomodel.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 toToList
will create aList
and selectingnew UserProductProperty
will make sure you get the type you want.
Hope this helps,
Fredrik
这篇关于将值传递给列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!