简体   繁体   English

如何避免多次运行同一循环?

[英]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.

相关问题 如何避免同一行在c#中添加多次 - how to avoid same line adding multiple times in c# 如何避免在同一个 scope 中多次重复输入相同的名称? - How to avoid repeating typing the same name multiple times within the same scope? 无限并行运行多次相同的ActionBlock - Running the same ActionBlock multiple times with Unbounded Parallelism 使用不同的参数多次运行同一查询 - Running the same query multiple times with different parameters Unity - 避免在同一对象上进行多次光线投射以多次运行功能 - Unity - avoid multiple raycasts on same object to run function multiple times 当测试调用其他方法的方法时,如何避免多次编写相同的测试 - How to avoid writing the same tests multiple times when testing methods that call other methods 如何使用 for 循环多次“返回”? - How to "return" multiple times with for loop? 避免在同一个DbContext上同时运行多个任务的实体框架错误 - Avoid Entity Framework Error with Multiple Tasks Running Concurrently on Same DbContext 在 C# 中使用不同的参数多次运行相同的方法 - Running the same method multiple times with different parameters in c# 与 EF Core 并行运行同一任务多次 - Running same task multiple times in parallel with EF Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM