即使我添加OrDefault

时间:2017-06-06 16:13:09

标签: c# asp.net-mvc linq

我的生活中有一个错误,我无法解决。 我得到的错误如下:

  

[InvalidOperationException]:序列中不包含任何元素   NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression   nhLinqExpression,IQuery查询,NhLinqExpression nhQuery)at   NHibernate.Linq.DefaultQueryProvider.Execute(表达式)   在NHibernate.Linq.DefaultQueryProvider.Execute [TResult](表达式   表达式)在System.Linq.Queryable.First [TSource](IQueryable 1 source) at Nichols.Web.Controllers.LoadController.RttInUse(String rttNumber) in c:\projects\NicholsFarms_demo_newCropYear\Nichols\src\Nichols.Web\Controllers\LoadController.cs:line 71 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2个参数)at at   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext   controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult)   asyncResult)at   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters&LT;&GT; c__DisplayClass46.b__3f()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker。&lt;&gt; c__DisplayClass33.b__32(IAsyncResult asyncResult)at at   System.Web.Mvc.Async.AsyncControllerActionInvoker&LT;&GT; c__DisplayClass21&LT;&GT; c__DisplayClass2b.b__1c()   在   System.Web.Mvc.Async.AsyncControllerActionInvoker&LT;&GT; c__DisplayClass21.b__1e(IAsyncResult的   asyncResult)at   System.Web.Mvc.Controller.b__1d(IAsyncResult的   asyncResult,ExecuteCoreState innerState)at   System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)at   System.Web.Mvc.MvcHandler.b__5(IAsyncResult的   asyncResult,ProcessRequestState innerState)at   System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult的   asyncResult)at   System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,   布尔和放大器; completedSynchronously)

我正在尝试使用的代码是:

public ActionResult RttInUse(string rttNumber)
{
    int validNumber;
    int recommendRttNumber = 0;
    var cropYear = _session.Query<CropYear>().SingleOrDefault(x => x.IsCurrent);

    if (cropYear.RttRangeHaveBeenConfiguredForNewCropYear)
    {
        recommendRttNumber = _session.Query<Load>()
            .Where(x =>
                x.CropYear == cropYear &&
                !x.RttNumber.Contains("VOID") &&
                x.RttNumber.Length < 9)
            .OrderByDescending(x => x.Id)
            .Select(x => x.RttNumber)
            .FirstOrDefault(x => int.Parse(x) >= cropYear.RttNumberRangeStart &&
                                 int.Parse(x) <= cropYear.RttNumberRangeEnd).AsInt() + 1;
    }

    if (rttNumber == null || !int.TryParse(rttNumber, out validNumber))
    {
        return AsJson(
            "Rtt number invalid, numeric values only. Next Rtt Number should be " +
            recommendRttNumber);
    }

    var loadExists = _session.Query<Load>()
        .Any(x =>
            x.RttNumber.ToLower() == rttNumber.ToLower() &&
            x.CropYear == cropYear);

    if (loadExists)
    {
        return AsJson("Rtt Number already in use. Next Rtt Number should be " +
                      recommendRttNumber);
    }

    return AsJson(true);
}

基本上,当运算符在表单上输入一个字符串(如150001)时,它们输入的值将与数据库中可接受的范围进行比较。

作物年份的条目使得2015年的可接受范围为150001至159999,可供选择RttNumberRangeStart至RttNumberRangeEnd;如果它正在使用,他们应该推荐当前使用的rttnumber + 1。

1 个答案:

答案 0 :(得分:2)

以下序列可能不包含任何元素:

<div id="nav_head1" class="nav_group" style="display: block;">
<div id="nav_arrow"><a href="javascript:toggleTree('pnl10','usersStatusView.html');"><img src="arrow_down.gif" id="apnl10" alt="" width="8" border="0" height="10"></a></div>
<div id="nav_icon"><a href="javascript:toggleTree('pnl10','usersStatusView.html');"><img src="icon_users.gif" alt="" width="20" border="0" height="20"></a></div>
<div id="nav_head1_text"><a href="javascript:toggleTree('pnl10','usersStatusView.html');" class="nav_font_head1">
Users</a></div>
</div>
<div id="nav_head2" class="nav_item" style="display: block;"><div id="nav_head2_textbox"><a href="usersSettingsView.html" onclick="javascript:hiLight(this);" target="tabFrame" class="nav_sub_sett">Settings</a></div>
</div>

因此它无法在null(默认)元素

上执行以下操作
.FirstOrDefault(x => int.Parse(x) >= cropYear.RttNumberRangeStart &&
                     int.Parse(x) <= cropYear.RttNumberRangeEnd)

假设.AsInt() + 1; RttNumberstring一种将AsInt()转换为string的扩展方法,请将其更改为返回int null 0