Why is this error, 'Sequence contains no elements', happening?

前端 未结 3 1408
离开以前
离开以前 2020-12-30 23:42

I am getting an Invalid Operation Exception, the stack is down below. I think it is because db.Responses.Where(y => y.ResponseId.Equals(item.ResponseId)).First();

3条回答
  •  无人及你
    2020-12-31 00:09

    Check again. Use debugger if must. My guess is that for some item in userResponseDetails this query finds no elements:

    .Where(y => y.ResponseId.Equals(item.ResponseId))
    

    so you can't call

    .First()
    

    on it. Maybe try

    .FirstOrDefault()
    

    if it solves the issue.

    Do NOT return NULL value! This is purely so that you can see and diagnose where problem is. Handle these cases properly.

提交回复
热议问题