首页 > 解决方案 > 从 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>”。存在显式转换(您是否缺少演员表?)

标签: c#.netlistienumerable

解决方案


我认为问题就在这里。

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

推荐阅读