Unable to retrieve Dictionary value when used in LINQ -
i trying update 'name' datacolumn based on 'id' datacolumn in datatable. 'id'-'name' pairs stored in dictionary, created using datatable extension method follows:
public static dictionary<object, object> todictionary(this datatable datatable, string keycolumn, string valuecolumn) { dictionary<object, object> resultdict = new dictionary<object, object>(); if (datatable.columns.contains(keycolumn) && datatable.columns.contains(valuecolumn)) { foreach (datarow dr in datatable.rows) if (resultdict.containskey(dr[keycolumn]) == false) resultdict.add(dr[keycolumn], dr[valuecolumn]); } return resultdict; }
i using following linq syntax update datacolumn:
misuserdetailsdt.asenumerable().tolist().foreach(row => row.setfield("departmentname", deptdict[row["departmentid"]].tostring()));
here 'misuserdetailsdt' datatable containing filled column 'departmentid' , empty column 'departmentname'-which trying update.
i have seperate method generates dictionary follows:
var deptdict = departmentdt.todictionary("deptid", "deptname");
where 'departmentdt' datatable containing columns 'deptid' , 'deptname'.
now when execute linq code give 'keynotfoundexception'. have thoroughly checked 'departmentdt' table 'deptdict' have keys(departmentids) 'misuserdetailsdt' table. 'departmentdt' has been generated distinct ids of 'misuserdetailsdt' table.
i think there issue figuring out appropriate type of dictionary keys. tried casting int16, didn't work out. please me figure out solution.
i have figured out problem. code works when dictionary values casted int32. @timschmelter giving useful info. changing code make use of foreach loop instead.
Comments
Post a Comment