string categoryIDList = Convert.ToString(reader["categoryIDList"]);
if (!String.IsNullOrEmpty(categoryIDList))
{
c.CategoryIDList =
new List<int>().AddRange(
categoryIDList
.Split(',')
.Select(s => Convert.ToInt32(s)));
}
The class has a property IList CategoryIDList that I am trying to assign to above.
Error:
Error 1 Cannot implicitly convert type 'void' to 'System.Collections.Generic.IList'
Not sure what the issue is?
You're assigning the result of AddRange to c.CategoryIDList, not the new list itself.
AddRange doesn't return a list - it returns void. You can do this via the constructor for
List<T>
that takes an enumerable:To have better understanding of what is going on, I created example below. Solution should be based on 1. list.AddRange, 2. then reassigning list to something else:
Why not initialize the list with the results of your select query instead of doing AddRange since it takes IEnumerable as an overload:
Your problem is that the AddRange method of the generic List class is declared as returning void.
Update: Edited to fix
List<int>
vs.IList<int>
issue.You need to change it to: