值不能为空。参数名称:第一个

当我运行下面的代码时,出现以下错误:


值不能为空。参数名称:第一个


这是我的代码:


private async void CallWebApiDetails()

{

    WebApiService oWS = new WebApiService();

    lstLocalDBAlertsID = new List<string>();


    try

    {

        ErrorHandle err = await oWS.GetAllAlerts();

        var lstdistinct = err.lstServerAlertsIDs.Except(lstLocalDBAlertsID).ToList();


        if (lstdistinct != null)

        {

            var lstOrderedLst = lstdistinct.OrderBy(i => i).ToList();


            if (lstOrderedLst.Count > 0)

            {

                for (int i = 0; i < lstOrderedLst.Count; i++)

                {

                    AlertData oAlertData = new AlertData();

                    oAlertData.Where.AlertId.Value = lstOrderedLst[i];


                    if (!oAlertData.Query.Load())

                    {

                        ErrorHandle oErr = new ErrorHandle();

                        oErr = await oWS.getSpecificAlert(lstOrderedLst[i]);

                        await SaveAlertData(Convert.ToInt32(lstOrderedLst[i]), oErr);

                    }

                }

            }

        }

    }

    catch (Exception ex)

    {

        LogError oLE = new LogError();

        oLE.logEx(ex, "CallWebApiDetails");

    }

}

有人可以告诉我我的代码有什么问题吗?


RISEBY
浏览 196回答 1
1回答

慕标琳琳

该Except扩展方法具有first作为this参数; 它被定义为public static IEnumerable<TSource> Except<TSource>(&nbsp; &nbsp; this IEnumerable<TSource> first, IEnumerable<TSource> second){&nbsp; &nbsp; if (first == null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw Error.ArgumentNull("first");&nbsp; &nbsp; }&nbsp; &nbsp; // ...}所以大概在这一行:var lstdistinct = err.lstServerAlertsIDs.Except(lstLocalDBAlertsID).ToList()的值err.lstServerAlertsIDs就是null。所以:解决这个问题。注意:最好将调试器与断点一起使用,或者至少要使用堆栈跟踪来确定哪条线失败,这是一个好主意。您不能总是(或什至经常)推断这样的上下文。
打开App,查看更多内容
随时随地看视频慕课网APP