c# - 从 IList 而不是 IEnumerable (C#) 获取数据
问题描述
当我得到:
public interface Test
{
IEnumerable<Guid> ModelIds { get; set; }
}
然后投入使用,我有:
public IEnumerable<Guid> Test
{
get => m_data.ModelIds;
set { m_data.ModelIds = value.ToList(); }
}
然后我像这样使用它:
abc.ModelIds = my_list.Select(x => x.Id);
但我需要对界面进行更改:
public interface Test
{
IList<Guid> ModelIds { get; set; }
}
和实施服务:
public IList<Guid> Test
现在怎么拿身份证?
abc.ModelIds = my_list.Select(x => x.Id);
错误 CS0266:无法将类型“System.Collections.Generic.IEnumerable<System.Guid>”隐式转换为“System.Collections.Generic.IList<System.Guid>”。存在显式转换(您是否缺少演员表?)
解决方案
我认为问题就在这里。
public IEnumerable<Guid> Test
{
get => m_data.ModelIds;
set { m_data.ModelIds = value.ToList(); }
}
而不是 .ToList() 只需使用 m_data.ModelIds = value;
如果它说问题就在这里。
abc.ModelIds = my_list.Select(x => x.Id);
尝试
abc.ModelIds = my_list.Select(x => x.Id).ToList();
推荐阅读
- javascript - 关于如何返回多个标签的css值的jQuery问题我尝试使用单个它返回很好但多个不是
- ios - 使用 Swift 在 PDF 中自动滚动
- c++ - 函数重载和调用
- git - 使用 ls 命令时,删除的文件夹仍然出现在 git bash 中
- scala - 如何正确地将会话值传递给方法 feed()
- html - 覆盖动画不应显示在底层 div 之外
- sql - 如何从 SQL 中的电话号码将电话号码前缀与国家/地区匹配
- php - 如何在 PHP 的 get_content 中循环 URL 查询字符串的值并回显相应的数据?
- hybris - hybris中业务流程和事件的区别
- python - 在 __init__ 参数中使用类 self.varname