Share via


Cannot implicitly convert type 'string' to 'System.Collections.Generic.List'

Question

Sunday, July 19, 2015 4:17 PM

How do you convert this?

List<int> myIntList = Request["requestedID"];

All replies (4)

Monday, July 20, 2015 12:33 AM âś…Answered

It would help if you took a look at what is coming over the wire.  I guess that it is something like "12,15,27".  Is Request["blah"] a string? or an object that needs to be cast to a string?  

 Depending on what is actually being sent you will need code like

var myInts = Request["requestedID"].Split(',').Select(s=>Int32.Parse(s));

(in this code myInts is not a List<Int32> but an IEnumerable<Int32> which is probably all you need)


Sunday, July 19, 2015 4:23 PM

ForumHelp7

List<int> myIntList = Request["requestedID"];

requestedID is not a List type collection rather it is a value (integer/string).

You can try

int retID = Convert.ToInt32(Request["requestedID"]);

or 

var retID = Request["requestedID"].ToString();

Hope this will help.


Sunday, July 19, 2015 4:32 PM

This is passed from javascript.

var requestedID= rID.join(",");
List<int> myIntList= Convert.ToInt32(Request["requestedID"]);

Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<int>'


Sunday, July 19, 2015 4:34 PM

ForumHelp7

List<int> myIntList= Convert.ToInt32(Request["requestedID"]);

The same issue here - you cannot assign a scaler value to List type collection.

cast the value as scaler and then try to add it to the list.

You can try as below:

List<int> myIntList = new List<int>();

int retID = Convert.ToInt32(Request["requestedID"]);

myIntList.Add(retID);