无法将类型void隐式转换为IList <int>

mrb*_*lah 0 c# oop

 string categoryIDList = Convert.ToString(reader["categoryIDList"]);

    if (!String.IsNullOrEmpty(categoryIDList))
    {
        c.CategoryIDList  =
            new List<int>().AddRange(
                categoryIDList 
                    .Split(',')
                    .Select(s => Convert.ToInt32(s)));

    }
Run Code Online (Sandbox Code Playgroud)

该类有一个属性IList CategoryIDList,我试图分配给上面.

错误:

错误1无法将类型'void'隐式转换为'System.Collections.Generic.IList'

不确定是什么问题?

Dan*_*den 5

您的问题是泛型List类AddRange方法被声明为返回void.

更新:编辑修复List<int>IList<int>问题.

您需要将其更改为:

List<int> foo = new List<int>();
foo.AddRange(
    categoryIDList 
    .Split(',')
    .Select(s => Convert.ToInt32(s)));
c.CategoryIDList = foo;
Run Code Online (Sandbox Code Playgroud)