C#: Get Single Item from a List<Custom_Class> If an item exist with one Property value="somevalue" in the list -
i have list of custom class(which has properties name,age,address).how can check whether have item "name" value "shyju" exist in list , return if exist.name unique.no 2 items have same name.
the solution thinking of go for each loop , loop thru each item , check name of each item "shyju" , return it.
is there other way ?
the following return item provided name if there 1 instance in list (it throw if there more 1 element).
var item = list.singleordefault(x => x.name=="shyju"); if ( item != null ) { ... }
it bit of work guarantee there 1 item in list name. if have established can speed bit using firstordefault
instead
var item = list.firstordefault(x => x.name=="shyju"); if ( item != null ) { ... }
Comments
Post a Comment