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

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -