如果不使用“占位符",我如何安全地循环直到无事可做?而条件? [英] How can I safely loop until there is nothing more to do without using a "placeholder" while conditon?

查看:18
本文介绍了如果不使用“占位符",我如何安全地循环直到无事可做?而条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了调用我的 Web API 方法直到没有更多数据返回(我分批获取它,以保持每个结果集很小,由于客户端(Windows CE 手持设备)的 98 磅弱角色)),我正在使用此代码:

In order to call my Web API method until no more data is returned (I'm fetching it in batches, to keep each result set small, due to the 98-lb-weakling persona of the client (Windows CE handheld device)), I'm using this code:

while (moreRecordsExist)
{
    redemptionsList.redemptions.Clear();
    string uri = String.Format("http://platypus:28642/api/Redemptions/{0}/{1}", lastIdFetched, RECORDS_TO_FETCH);
    var webRequest = (HttpWebRequest)WebRequest.Create(uri);
    webRequest.Method = "GET";

    using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
        if (webResponse.StatusCode == HttpStatusCode.OK)
        {
            var reader = new StreamReader(webResponse.GetResponseStream());
            string s = reader.ReadToEnd();
            var arr = JsonConvert.DeserializeObject<JArray>(s);
            if (null == arr) break;

            foreach (JObject obj in arr)
            {
                id = obj.Value<int?>("Id") ?? 0;
                var _redemptionId = obj.Value<string>("RedemptionId") ?? "";
                var _redemptionItemId = obj.Value<string>("RedemptionItemId") ?? "";
                var _redemptionName = obj.Value<string>("RedemptionName") ?? "";
                double _redemptionAmount = obj.Value<double?>("RedemptionAmount") ?? 0.0;
                var _redemptionDept = obj.Value<string>("RedemptionDept") ?? "";
                var _redemptionSubdept = obj.Value<string>("RedemptionSubDept") ?? "";

                redemptionsList.redemptions.Add(new HHSUtils.Redemption
                {
                    Id = id,
                    RedemptionId = _redemptionId,
                    RedemptionItemId = _redemptionItemId,
                    RedemptionName = _redemptionName,
                    RedemptionAmount = _redemptionAmount,
                    RedemptionDept = _redemptionDept,
                    RedemptionSubDept = _redemptionSubdept,
                });
            } // foreach
        } // if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 2))
    } // using HttpWebResponse
    int recordsAdded = LocalDBUtils.BulkInsertRedemptions(redemptionsList.redemptions);
    totalRecordsAdded += recordsAdded;
    //moreRecordsExist = (recordsToFetch > (totalRecordsAdded));
    lastIdFetched = id;
} // while

这有效(如果我检查 webResponse 是否为 null,它会因 NullReferenceException 崩溃),但我真的不喜欢我的 while 循环,因为一旦 moreRecordsExist 被分配为 false,它就永远不会到达.因此,我将 Resharper 猎犬放在上面,看看它是否有更好的建议,但它只是告诉我,该行的表达式始终为真",建议的修复"是将其更改为同时(真)"

This works (if I check webResponse for null, it crashes with a NullReferenceException), but I don't really like my while loop, as it is never reached once moreRecordsExist is assigned false. So, I set the Resharper hounds loose on it to see if it had a better suggestion, but it just tells me, "Expression is always true" for that line, and the suggested "fix" for this is to change it to "while (true)"

不过,我看不出这有多大的改进.

I fail to see how that is much of an improvement, though.

有没有办法用更明智的结构来完成同样的事情?

Is there a way I can accomplish the same thing with a more sensible construct?

推荐答案

在将 false 分配给 moreRecordsExist<后立即使用 break 语句中断循环/code> 变量,所以它的值在循环开始时永远不会为假.您可以按照 ReSharper 的建议进行更改并删除 moreRecordsExist 变量:

You break out the loop with a break statement immediately after assigning false to moreRecordsExist variable, so its value will never be false in the beginning of the loop. You can make a change as ReSharper suggests and get rid of moreRecordsExist variable:

while (true)
{
    redemptionsList.redemptions.Clear();
    string uri = String.Format("http://platypus:28642/api/Redemptions/{0}/{1}", lastIdFetched, RECORDS_TO_FETCH);
    var webRequest = (HttpWebRequest)WebRequest.Create(uri);
    webRequest.Method = "GET";

    using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
        if (webResponse.StatusCode == HttpStatusCode.OK)
        {
            var reader = new StreamReader(webResponse.GetResponseStream());
            string s = reader.ReadToEnd();
            var arr = JsonConvert.DeserializeObject<JArray>(s);
            if (arr == null) break;

            foreach (JObject obj in arr)
            {
                id = obj.Value<int?>("Id") ?? 0;
                var _redemptionId = obj.Value<string>("RedemptionId") ?? "";
                var _redemptionItemId = obj.Value<string>("RedemptionItemId") ?? "";
                var _redemptionName = obj.Value<string>("RedemptionName") ?? "";
                double _redemptionAmount = obj.Value<double?>("RedemptionAmount") ?? 0.0;
                var _redemptionDept = obj.Value<string>("RedemptionDept") ?? "";
                var _redemptionSubdept = obj.Value<string>("RedemptionSubDept") ?? "";

                redemptionsList.redemptions.Add(new HHSUtils.Redemption
                {
                    Id = id,
                    RedemptionId = _redemptionId,
                    RedemptionItemId = _redemptionItemId,
                    RedemptionName = _redemptionName,
                    RedemptionAmount = _redemptionAmount,
                    RedemptionDept = _redemptionDept,
                    RedemptionSubDept = _redemptionSubdept,
                });
            } // foreach
        } // if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 2))
    } // using HttpWebResponse
    int recordsAdded = LocalDBUtils.BulkInsertRedemptions(redemptionsList.redemptions);
    totalRecordsAdded += recordsAdded;
    //moreRecordsExist = (recordsToFetch > (totalRecordsAdded));
    lastIdFetched = id;
} // while

这篇关于如果不使用“占位符",我如何安全地循环直到无事可做?而条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆