C# 需要递归帮助
在我们的 Windows 应用程序中,我们有 startDate 和 EndDate。单击“执行”按钮事件时,我们需要使用搜索字符串 + 日期范围(日期从 01/01/2010 到 12/31/2010)调用第三方 Web 服务。现在,我们的搜索条件可以返回数千条记录,但 Web 服务有限制,每次交易只能返回 10K 条记录。
这需要我们分解我们的日期范围。所以基本上我们需要以下内容;
对于 (X dateRange if RecordCount > 10000) 那么 X dateRange/2 在我们的例子中为 01/01/2010 到 06/01/2010 并再次检查条件并递归执行此操作,直到我们得到 RecordCount < 的 daterange 块。 10000。
然后从下一个日期开始,例如,如果我们获得 01/01/2010 到 03/30/2010 的 9999 条记录,那么我们需要获取从 04/01/2010 开始的下一个块的记录,
这可以通过递归实现吗?
RecursionFunction(dtStart, dtEnd)
{
if (WebService.RecordCount > 9999)
{
TimeSpan timeSpan = dtEnd.Subtract(dtStart);
DateTime mStart = dtStart;
DateTime mEnd = dtStart.AddDays(timeSpan.Days / 2);
RecursionFunction(dtStart,dtEnd);
}
else
{
Get Records here
}
}
但是使用上面的代码,递归将具有以下块
01/01/2010, 12/31/2010 > 10000 01/01/2010, 07/03/2010 > 10000 2010年1月1日、2010年2月4日< 10000
因此,在完成获取记录后,递归将从我们不需要的块 01/01/2010,07/03/2010 重新开始。我们需要从 04/03/2010,12/31/2010 开始下一次递归,
提前感谢您的帮助。
On our windows application, We have startDate and EndDate. On click of Execute button event, we need to call a third party web service with our search string + daterange( date from 01/01/2010 to 12/31/2010). Now our search criteria can return us thousands of records but web service have limitation of able to return only 10K records per transaction.
Which required us to break down our dateRange. So basically we need following;
For (X dateRange if RecordCount > 10000) then
X dateRange/2 which will be 01/01/2010 to 06/01/2010 in our case and check condition again and do this recursively until we get daterange block where RecordCount is < 10000.
Then start with Next date, for example, if we get 9999 records for 01/01/2010 to 03/30/2010 then we need to get records for next block starting 04/01/2010
Is this possible with Recursion?
RecursionFunction(dtStart, dtEnd)
{
if (WebService.RecordCount > 9999)
{
TimeSpan timeSpan = dtEnd.Subtract(dtStart);
DateTime mStart = dtStart;
DateTime mEnd = dtStart.AddDays(timeSpan.Days / 2);
RecursionFunction(dtStart,dtEnd);
}
else
{
Get Records here
}
}
But with above code, recursion will have following blocks
01/01/2010, 12/31/2010 > 10000
01/01/2010, 07/03/2010 > 10000
01/01/2010, 04/02/2010 < 10000
So after finishing getting record, recursion will start again with block 01/01/2010,07/03/2010 which we don't need. We need to start next recursion with 04/03/2010,12/31/2010
Thanks in advance for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看起来您正在尝试分割输入范围,直到它小到足以处理。尝试为两个范围调用它:
It looks like you are trying to split the input range until it is small enough to handle. Try calling it for both ranges:
第一步是将 RecursionFunction 调用(在示例的第 8 行)更改为:
但是,您还需要使用日期范围的另一半再次调用它。
此外,您还需要处理结果(大概结合两个答案)。
The first step is to change the RecursionFunction call (at line 8 of your example) to:
But, then, you'll also need to call it again with the other half of the date range.
Also, you need to handle the results (presumably combining the two answers).
这就像分而治之。您需要从拆分的左侧和右侧获取结果并将它们组合并返回该值。因此,您可以继续缩小规模,直到有足够的数据可以处理并返回。然后继续将结果集连接在一起。
This is like divide and conquer. You need to get results from the left and the right of the split and combine them and return that value. So you can keep getting smaller until you have enough data you can deal with and just return that. Then keep joining the result sets together.
这就是我的做法
注意,无法测试
它的工作原理是将日期分成两半,然后搜索每个日期,如果其中一个仍然大于 9999,则再进行一次。
This is how I would do it
Note, Couldn't test it
It works by dividing the dates in half, then searching each of them, and if one is still bigger than 9999, then doing it again.
一种简单的方法是分页形式。如果您使用 JSON 或 XML,您可以输入总结果的数量,然后只返回一组结果(也返回偏移量)。这样,您可以执行一个循环来检查您是否在最后一页,并在获得最后一个结果页后,退出它。
如果特定交易失败,请不要忘记进行检查。对于如此大的数据集,这不是一个理想的解决方案,但它是一种解决方法
An easy way would be a form of pagination. If your using JSON or XML, you can put the amount of total results and just return a set number of results (return the offset too). This way you can do a loop to check if your on the last page and after you get the last results page, break out of it.
Don't forget to put checks in if a particular transaction fails though. It's not an ideal solution on such a large dataset but it is a workaround
重用在 while 循环中实际返回的数据的最后日期听起来比像这样的递归要容易得多。
3 月有 31 天。
伪 C# 代码
为了减轻服务的负载,您可以计算上一次运行的时间跨度,并在 while 循环中仅获取下一次运行的天数。
如何处理不可避免的双峰取决于记录中的数据。
我刚刚意识到我假设您在返回的数据中有一个日期。如果不是,则忽略此答案。
It sounds much easier to just reuse the last date for the data you actually got back in a while-loop than to home in with recursion like this.
March has 31 days.
Pseudo-C# code
To ease the load on the service you could calculate the timespan for the previous run and only get that many days for the next run in the while-loop.
How you should handle the inevitable doublets depends on the data on the records.
I just realized I presumed you had a date in the returned data. If not, then disregard this answer.