linq - How do Group by a child property of a child property that may be null? -


how go grouping following?

people.groupby(p=>p.addresses.getfirstordefault().state); 

without failing on people don't have address?

  • can done in 1 statement?
  • if not, have first distinct() list of various addresses members? how? (actually -- if possible -- great learn how b :-) )
  • i havn't seen it, there equivalent getfirstornew() can used instantiate , return non-null?

thank much!

it can done in 1 statement, yes:

// set whatever need address dummyaddress = new address { state = "" };  people.groupby(p => (p.addresses.getfirstordefault() ?? dummyaddress).state); 

alternatively, might want write helper method:

public string getstate(address address) {     return address == null ? null : address.state; } 

then can use:

people.groupby(p => getstate(p.addresses.getfirstordefault())); 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -