muhd
muhd

Reputation: 11

Error casting in C#

I'm getting the following error

System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'.

in the code:

public static List<Category> getPopularCategories() {
    clsCityPageDataContext cox = new clsCityPageDataContext();
    var cats = from cat in cox.Categories select cat;
    return cats.ToList<Category>().GetRange(0,10);
}

Upvotes: 1

Views: 390

Answers (1)

JTorrecilla
JTorrecilla

Reputation: 208

The problem is:

var cats = from cat in cox.Categories select cat;
return cats.ToList<Category>().GetRange(0,10);

replace with:

var cats = from cat in cox.Categories select cat;
return cats.ToList().GetRange(0,10);

Upvotes: 1

Related Questions