开发者

IEnumerable not null but calling Count() results in Exception

开发者 https://www.devze.com 2023-01-01 19:19 出处:网络
I have code that\'s working beautifully on my development machine, but when deployed to the server is throwing null reference exception. So, I can\'t step through the code, but I\'ve pinned down the c

I have code that's working beautifully on my development machine, but when deployed to the server is throwing null reference exception. So, I can't step through the code, but I've pinned down the culprit. But now I am puzzled. Here's the code. The question is below.

Dim certs = From p In persons _
        Select New Certificate( _
        p.Value, _
        New CertificateData.Seminar(thisEvent.Seminar.Name, _
               thisEvent.StartDate.Value, _
               thisEvent.EndDate.Value, _
               thisEvent.Venue.City, _
               thisEvent.Venue.State, _
               New CertificateData.Instructor( _
               staffMember.Name, _
               staffMember.Titles, _
               instrSignatPath))) _
        With {.CertificateId = p.Key}

lblMessage.Text = CStr(certs Is Nothing)
lblMessage.Text = lblMessage.Text + "<br />" + CStr(certs.Count())

In the code above persons is a dictionary of custom class, and certs开发者_开发技巧 is of IEnumerable type. Now here's the quandary.. The first line that sets the label returns False, so certs is not null. But the second line throws an null reference exception. How is this possible?


My guess is because of the deferred execution of LINQ. The items are enumerated only when you call Count() and if your code in the expression fails, you'll get the exception right there.

I'm pretty sure that at least one of staffMember, thisEvent, thisEvent.Seminar or thisEvent.Venue are null.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号