[英]How to avoid running the same loop multiple times?
Here is a big problem for me, because I can't get out of the loop. 这对我来说是个大问题,因为我无法摆脱困境。 I got in listDataReport.Count
only 1. But in the foreach
from beginDate
(example: 10/01/2010) to endDate
(today), my file writes as times as date are going through from the beginning to the end. 我仅进入listDataReport.Count
。但是在从beginDate
(例如:10/01/2010)到endDate
(今天)的foreach
,我的文件写入的时间是从开始到结束。
What I want is only one write for the specific date in listDataReport
我想要的只是listDataReport
特定日期的一次写入
listDataReport = blReports.GetDataClientes(beginDate, endDate);
string dateSelected = string.Empty;
while (beginDate < endDate)
{
foreach (var item in listDataReport)
{
FileCreated.WriteLog(item.NmbrCliLeg + "|" + item.NmbCliProf + "|" + item.Namsur + "|" + item.Mail, false);
dateSelected= item.DateUp;
}
beginDate = beginDate.AddDays(1);
}
log.WriteLog("Clients up date: " + dateSelected + ":" + listDataReport.Count);
It repeats as many days are between beginDate
- endDate
. 在beginDate
- endDate
之间重复许多天。 And if i only get one Client on listDataReport
that happens. 如果我只在listDataReport
上获得一个Client,则会发生这种情况。
As you can see there's FileCreated.WriteLog
creates a .txt which writes the data client here's the problem. 如您所见,这里有FileCreated.WriteLog
创建一个.txt文件,它将数据客户端写入其中,这就是问题所在。 In log.WriteLog
which creates a .log file there's no problem in that. 在创建.log文件的log.WriteLog
,这没有问题。 I'll show what I got from both files. 我将展示两个文件的内容。
In log: 在日志中:
---***--- Execution Initiated: 27/03/2015 09:44:40 a.m.
27/03/2015 09:44:50 a.m. - Clients up date 03/19/2015: 1
===***=== Execution Ended: 03/27/2015 09:44:50 a.m.
But now in .txt file (Datefile03272015.txt) : 但是现在在.txt文件(Datefile03272015.txt)中:
0123456789|7976967|NAME SURNAME|somemail@mail.com
0123456789|7976967|NAME SURNAME|somemail@mail.com
0123456789|7976967|NAME SURNAME|somemail@mail.com
0123456789|7976967|NAME SURNAME|somemail@mail.com
All the job runs each day and creates a .txt file each day. 所有作业每天运行,并每天创建一个.txt文件。 It doesn't matter the .log file here. 此处的.log文件无关紧要。
Use the keyword break to break out of a loop: 使用关键字break可以打破循环:
https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx https://msdn.microsoft.com/zh-CN/library/ttw7t8t6.aspx
int[] list = [1, 2, 3, 4, 5, 6, 7, 8];
foreach(int item in list)
{
Console.WriteLine(item);
if (item == 5)
break;
}
Output: 输出:
1
2
3
4
5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.