FirstOrDefault: Default value other than null

后端 未结 11 1149
-上瘾入骨i
-上瘾入骨i 2020-12-12 21:37

As I understand it, in Linq the method FirstOrDefault() can return a Default value of something other than null. What I haven\'t worked out is wha

11条回答
  •  时光说笑
    2020-12-12 22:14

    You can use DefaultIfEmpty followed by First:

    T customDefault = ...;
    IEnumerable mySequence = ...;
    mySequence.DefaultIfEmpty(customDefault).First();
    

提交回复
热议问题