私は次のコードを持っています。null値を検索すると、EF内のLINQが失敗しますか?
//I pick the first story where its StartSegment is not null.
var story = container.StorySet.FirstOrDefault(s => s.StartSegment != null);
if (story != null)
{
//the following assert fails because story.StartSegment is null.
Assert.IsNotNull(story.StartSegment,
"The target story of this homework has no start segment.");
}
story.StartSegmentが実際のヌルであるため、このユニットテストは、失敗しますが、明示的に開始セグメントが nullではない話を検索FirstOrDefaultのラムダ式を与え、私はこれを考えていません意味があります。
誰でも手伝ってもらえますか?
をあなたは 'First'の代わりに'使用する必要がありますFirstOrDefault'この場合 – Snowbear
しかし、なぜですか?なぜこれが必要なのか説明できますか?違いは何ですか? –
要素が見つからなかった場合、firstまたはdeafultはnullを返します。その要素をチェックする必要があります。 – Tsabo