tags:

views:

480

answers:

4
 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?

A: 

You're assigning the result of AddRange to c.CategoryIDList, not the new list itself.

tsellon
+4  A: 

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:

List<int> foo = new List<int>();
foo.AddRange(
    categoryIDList 
    .Split(',')
    .Select(s => Convert.ToInt32(s)));
c.CategoryIDList = foo;
Daniel Pryden
Arghh, IList<int> doesn't support AddRange though? c.CategoryIDList is of type IList<int>
mrblah
Yes, this won't compile as-is.
Reed Copsey
Daniel Pryden
+2  A: 

Why not initialize the list with the results of your select query instead of doing AddRange since it takes IEnumerable as an overload:

c.CategoryIDList = new List<int>(categoryIDList.Split(',')
 .Select(s => Convert.ToInt32(s)));
Roatin Marth
+1  A: 

AddRange doesn't return a list - it returns void. You can do this via the constructor for List<T> that takes an enumerable:

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

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