linq - Cast List<object> to List<myclass> c# -
by code got list of selected rows in aspxgrid.
string[] fieldname = new string[] { "userid", "name", "address" }; list<object> selectedlist = grid.getselectedfieldvalues(fieldname);
now want perform 1 of below operation.
- filter list of object userid userid = 1 using linq
- cast
list<object>
list<users>
i have tried following 2 methods exception occurs.
unable cast object of type 'system.object[]' type 'cubedataobject.claims'.
list<users> mylist = (list<users>)(object)selectedlist; list<users> listd = selectedlist.select(n => (users)n).tolist();
i have tried many other methods tired.
it seems have list of boxed object[]
. so, need unbox , values of userid
, name
, address
corresponding index.
here example:
list<users> mylist = selectedlist .where(item => (int)((object[])item)[0] == 1) .select(item => { var values = (object[])item; return new users() { userid = (int)values[0], name = (string)values[1], address = (string)values[2] }; }) .tolist();
Comments
Post a Comment