When to use .First and when to use .FirstOrDefault with LINQ?

后端 未结 14 1667
无人共我
无人共我 2020-11-22 09:09

I\'ve searched around and haven\'t really found a clear answer as to when you\'d want to use .First and when you\'d want to use .FirstOrDefault wit

14条回答
  •  礼貌的吻别
    2020-11-22 09:34

    Ok let me give my two cents. First / Firstordefault are for when you use the second constructor. I won't explain what it is, but it's when you would potentially always use one because you don't want to cause an exception.

    person = tmp.FirstOrDefault(new Func((p) =>
    {
        return string.IsNullOrEmpty(p.Relationship);
    }));
    

提交回复
热议问题