c# - Using list item contents as string name -
i'm using c# , making lists sqlite query responses.
list1 contains field name of field in list2
list1[i].field = fieldname
i want use fieldname stored in list1[i].field
access item in list2
like:
list2[ii].+list1[i].field+
i have no idea if possible in way in c# i'm relatively new , going off knowledge of php work.
for type of data structure, use dictionary (key/value pairs) dynamically select properties of object. specific example, accomplished reflection, not commonly used since other structures make easier.
list2[ii].gettype().getproperty(list1[i].field).getvalue(list2[ii], null);
reflection isn't fastest type of solution typically, , it's not great come in behind , maintain code lot of reflection if can avoided.
alternatively, use linq transfer list2 list of object types list of dictionary objects list2[ii][list1[i].field]
accomplish same thing.
var listdictionaries = list2 .select(l => new dictionary<string, object>() { new { "prop1", l.prop1 }, new { "prop2", l.prop2 }, new { "prop3", l.prop3 }, }).toarray(); listdictionaries[ii][list1[i].field];
Comments
Post a Comment