c# - Getting an IList<field_type> from IList<object_type> -
i have ilist<person> object. person class has fields, firstname, lastname, etc.
i have function takes ilist<string> , want pass in list of firstnames in same order of ilist<person> object. there way without building list of names person list?
what if change function take different parameter (other ilist<person>, function specific strings, not persons), maybe ienumerable<string>? i'd rather use ilist<string> though.
enumerable.select return ienumerable of strings (first names) you.
http://msdn.microsoft.com/en-us/library/bb548891.aspx
myfunction(people.select(o => o.firstname))
you add in tolist() if want pass in list
myfunction(people.select(o => o.firstname).tolist())
enumerable.select method introduced in c# 3 part of linq. read more linq here. see brief explanation of deferred execution here.
Comments
Post a Comment