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
Post a Comment