0

我收到一个错误,我不知道如何解决?它声称发生在:

var respondents = RespondentRepository.GetRespondents(UserSession, fieldsToInclude).Where(r=>r.Email.Contains(query)).ToList();

当我删除所有 .Where 东西时,它似乎工作正常。我真的不习惯这个错误,所以我不确定最好的开始在哪里?

错误:

System.NullReferenceException: Object reference not set to an instance of an object.

代码:

MakeSafeRequest(() =>
                {
                    var respondents = RespondentRepository.GetRespondents(UserSession, fieldsToInclude).Where(r=>r.Email.Contains(query)).ToList();

                    model = new RespondentSearchViewModel
                    {
                        Respondents = respondents,

                        TableData = respondents.SelectToListOrEmpty(r =>
                        {
                            return new TableRowViewModel
                            {
                                Id = r.Id,
                                Data = new Dictionary<string, string>
                                {
                                    { "FirstName", r.FirstName },
                                    { "LastName", r.LastName },
                                    { "EmailAddress", r.Email },
                                    { "Project", r.ProjectId.ToString() },
                                    { "Status", r.RecruitingStatus.ToIconHtml() },
                                    { "ViewHistory", "<div class=\"btnBlue btSml\"><a class=\"closeMe\" href=\"/RespondentSearch/RespondentDetails/?respondentId="+r.Id +"\" >view detail</a></div>"}
                                    //{ "StatusDate", r.LastActionDate.Value.ToLocalTime().ToString() },
                                }
                            };
                        })
                    };
                });

模型:

public class RespondentSearchViewModel:ListPageViewModel
    {
        public List<Respondent> Respondents { get; set; }
    }
4

1 回答 1

1

r.Email可能为空。尝试将您的 lambda 更改为:

r => (r.Email == null ? false : r.Email.Contains(query))
于 2013-11-07T19:28:40.813 回答