Visual C# check type of array elements with lambda expression -
i have array contains various type of elements.
i trying construct lambda expression evaluate if clause true if specific type exists in array.
i trying following , many different approaches don't seem right:
var arraysourcetext = editorcontroller.activedocument .activesegmentpair .source .allsubitems .toarray(); if (arraysourcetext.any(o => o.gettype()) == typeof(string)) { intstartingph++; }
any advice welcome.
note: arraysourcetext
api ienumerable
custom types. assumed string
simplicity in pseudocode.
update: because type not exposed api, name of type, workaround based on byyo's correction. wrote separate method check name of type , return bool
:
var arraysourcetext = editorcontroller.activedocument .activesegmentpair .source .allsubitems .toarray(); string typeph = "placeholdertag"; if (arraysourcetext.any(o => ismatchtypestring(typeph, o))) { intstartingph++; } //then below method check: static bool ismatchtypestring(string testtype, params object[] items) { if (items.length == 0) { return false; } else if (testtype == items[0].gettype().tostring()) { return true; } else { return false; } }
there seems error in parenthesis
v v if (arraysourcetext.any(o => o.gettype()) == typeof(string)) // approach if (arraysourcetext.any(o => o.gettype() == typeof(string))) // correct approach
instead of checking each element string
, check result of any()
typeof(string)
Comments
Post a Comment