0

I need a drop down list to display a current set of active cases, but I get an error whenever it returns with more than one case. The code looks like this:

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();

And the error I get is:

System.NullReferenceException: Object reference not set to an instance of an object. at PCM_UI.manageReferrals.b__2(CaseDTO x) in c:\Users\Public\Documents\PathFinder Case Manager\PCM.UI\pages\manageReferrals.aspx.cs:line 33 at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at PCM_UI.manageReferrals.Page_Load(Object sender, EventArgs e) in c:\Users\Public\Documents\PathFinder Case Manager\PCM.UI\pages\manageReferrals.aspx.cs:line 33

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 メソッドが反復を強制すると、null 参照があるようです。

于 2013-07-23T14:40:02.223 に答える