【发布时间】:2014-08-26 22:05:09
【问题描述】:
我已经定义了以下内容:
public ICollection<Item> Items { get; set; }
当我运行这段代码时:
Items = _item.Get("001");
我收到以下消息:
Error 3
Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<Storage.Models.Item>' to
'System.Collections.Generic.ICollection<Storage.Models.Item>'.
An explicit conversion exists (are you missing a cast?)
谁能解释我做错了什么。我很困惑 Enumerable、Collections 和使用 ToList() 的区别
添加信息
稍后在我的代码中,我有以下内容:
for (var index = 0; index < Items.Count(); index++)
我可以将 Items 定义为 IEnumerable 吗?
【问题讨论】:
-
你能提供更多关于_item的类型和Get(string)的签名(特别是返回类型)的信息吗?
-
为什么不这样改类型呢?
public IEnumerable<Item> Items { get; set; }ICollection有什么特别的原因吗? -
IEnumerable
Get(string pk);