4

我有以下查询:

   dat =
from s in dat
orderby s.Schedule.Employee.EmployeName ascending
select s;

问题是ScheduleEmployee可能是null,在这些情况下,它应该只显示第一个或最后一个,我不在乎。

有没有办法做到这一点?目前它在null.

用于对网格视图进行排序:

[NullReferenceException: Object reference not set to an instance of an object.]
   KezberProjectManager.Cases.<gvCases_Sorting>b__8(Case s) in C:\Users\jlarouche\documents\visual studio 2010\Projects\KezberProjectManager\KezberProjectManager\Cases.aspx.cs:103
   System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count) +88
   System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count) +21
   System.Linq.<GetEnumerator>d__0.MoveNext() +178
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +1698
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +67
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +123
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   System.Web.UI.WebControls.GridView.DataBind() +4
   KezberProjectManager.Cases.gvCases_Sorting(Object sender, GridViewSortEventArgs e) in C:\Users\jlarouche\documents\visual studio 2010\Projects\KezberProjectManager\KezberProjectManager\Cases.aspx.cs:138
   System.Web.UI.WebControls.GridView.OnSorting(GridViewSortEventArgs e) +122
   System.Web.UI.WebControls.GridView.HandleSort(String sortExpression, SortDirection sortDirection) +65
   System.Web.UI.WebControls.GridView.HandleSort(String sortExpression) +78
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +497
   System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +205
   System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642898
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
4

4 回答 4

4

您需要null在访问属性之前检查:

var x = from s in dat
        let s = s.Schedule
        let e = s == null ? null : s.Employee
        orderby e == null ? null : e.EmployeeName ascending
        select s;
于 2013-02-13T21:22:08.540 回答
0

像这样的东西?

dat =
    from s in dat
    orderby 
       s.Schedule == null ? "" : 
       s.Schedule.Employee == null ? "" : 
       s.Schedule.Employee.EmployeName ascending
    select s;
于 2013-02-13T21:22:38.033 回答
0

那么为什么不使用 if 条件来检查这些对象中的空值并在那里处理呢?我猜这里最大的问题是它们是否应该为空?如果是这样,听起来应该阻止此代码在应用程序中进一步运行。

于 2013-02-13T21:23:15.443 回答
-1

您有FirstOrDefault(),LastOrDefault()DefaultIfEmpty()方法来解决这种情况。

于 2013-02-13T21:21:07.977 回答