문제

If

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

Person p = per.???

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top