Frage

If

Ienumerable<Person> per = _serviceToGetPerson(); happens to only return one 'per' is it possible to assign it to a Person.

Person p = per.???

War es hilfreich?

Lösung

You can use First/FirstOrDefault.

Person p = per.FirstOrDefault();

FirstOrDefault would return the first Object from the list or null if the List is empty.

You can also use Single / SingleOrDefault, if you are expecting only one item back from the list or only one or null (for SingleOrDefault)

Make sure that you have included using System.Linq; namespace

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top