相关疑难解决方法(0)

无法从IEnumerable <T>转换为ICollection <T>

我已经定义了以下内容:

public ICollection<Item> Items { get; set; }
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时:

Items = _item.Get("001");
Run Code Online (Sandbox Code Playgroud)

我收到以下消息:

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?)
Run Code Online (Sandbox Code Playgroud)

有人可以解释我做错了什么.我对Enumerable,Collections和使用ToList()之间的区别感到很困惑

添加信息

稍后在我的代码中我有以下内容:

for (var index = 0; index < Items.Count(); index++) 
Run Code Online (Sandbox Code Playgroud)

我可以将Items定义为IEnumerable吗?

.net c#

80
推荐指数
2
解决办法
7万
查看次数

标签 统计

.net ×1

c# ×1