ICollection - Get single value

后端 未结 5 1043
孤街浪徒
孤街浪徒 2021-02-03 17:45

What is the best way to get a value from a ICollection? We know the Collection is empty apart from that.

5条回答
  •  感动是毒
    2021-02-03 18:03

    If you know your collection has only one item, should only ever have one item, you can use the Linq extension method Single().

    This converts a ICollection into a T object containing the single item of that collection. If the length of the collection is 0, or more than one, this will throw an InvalidOperationException.

提交回复
热议问题