我怎样才能安全地循环,直到有没有更多不使用&QUOT做;占位符"而conditon? [英] How can I safely loop until there is nothing more to do without using a "placeholder" while conditon?

查看:151
本文介绍了我怎样才能安全地循环,直到有没有更多不使用&QUOT做;占位符"而conditon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了调用我的Web API方法,直到返回没有更多的数据(我取它批次,以保持每个结果集小,由于客户端的98-LB-弱者角色(Windows CE的掌上设备)),我用这code:

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分配假它从来没有达到过。所以,我设置了ReSharper的猎狗失去它,看它是否有一个更好的建议,但它只是告诉我,前pression始终为真为线,并建议修复这是改变它,而(真)

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?

推荐答案

您打破了以破环语句指派后,立即 moreRecordsExist 变量,所以它的价值永远不会在循环的开头假的。你可以做一个变化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

这篇关于我怎样才能安全地循环,直到有没有更多不使用&QUOT做;占位符&QUOT;而conditon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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