Question

If

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

Person p = per.???

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top