0

我需要一个下拉列表来显示一组当前的活动案例,但是每当它返回多个案例时都会出现错误。代码如下所示:

masterCaseList.DataSource = MasterCasesBLL.GetAllMasterCases(false)
    .Where(x => x.MainContact.MainContact == true)
    .Select(x => new { MainContact = x.MainContact.MainContactLabel, index = x.ID })
    .ToList();

masterCaseList.DataValueField = "index";
masterCaseList.DataTextField = "MainContact";
masterCaseList.DataBind();

我得到的错误是:

System.NullReferenceException:对象引用未设置为对象的实例。在 c:\Users\Public\Documents\PathFinder Case Manager\PCM.UI\pages\manageReferrals.aspx.cs: PCM_UI.manageReferrals.b__2(CaseDTO x) 中的 PCM_UI.manageReferrals.b__2(CaseDTO x):System.Linq.Enumerable.WhereSelectListIterator 2.MoveNext() at System.Collections.Generic.List1..ctor的第 33 行(IEnumerable 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 来源)在 c:\Users\Public\Documents\PathFinder Case Manager\PCM.UI\pages\manageReferrals.aspx.cs:line 33 中的 PCM_UI.manageReferrals.Page_Load(Object sender, EventArgs e)

4

1 回答 1

2

您应该尝试在查询中添加以下内容:

masterCaseList.DataSource = MasterCasesBLL.GetAllMasterCases(false)
    .Where(x => x.MainContact != null && x.MainContact.MainContact == true)
    .Select(x => new { MainContact = x.MainContact.MainContactLabel, index = x.ID })
    .ToList();

当 ToList 方法强制迭代时,您似乎有一个空引用。

于 2013-07-23T14:40:02.223 回答