0

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.

What I want is only one write for the specific date in 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. And if i only get one Client on listDataReportthat happens.

As you can see there's FileCreated.WriteLogcreates a .txt which writes the data client here's the problem. In log.WriteLog which creates a .log file there's no problem in that. 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) :

0123456789|7976967|NAME SURNAME|[email protected]
0123456789|7976967|NAME SURNAME|[email protected]
0123456789|7976967|NAME SURNAME|[email protected]
0123456789|7976967|NAME SURNAME|[email protected]

All the job runs each day and creates a .txt file each day. It doesn't matter the .log file here.

8
  • 2
    Just remove the while loop? Commented Mar 27, 2015 at 13:14
  • 1
    Or we didn't understand your need, in which case, you should edit your post, or removing the while loop will do the job as said Henrik Commented Mar 27, 2015 at 13:15
  • You are nesting your loops, so you are iterating the entire listDataReport list for each date - is this the intent? Commented Mar 27, 2015 at 13:16
  • @DStanley yes it's a loop for each day, but if I found one client added one day for each day since select until today writes the same data, and I only write once, not anymore. The same if I found 499 clients in one day writes 499 times for each day. Do you understand? Commented Mar 27, 2015 at 13:28
  • 3
    @PabloCalderón We understand what the code does. We don't understand what you want it to do. Commented Mar 27, 2015 at 13:31

1 Answer 1

3

Use the keyword break to break out of a loop:

https://msdn.microsoft.com/en-us/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
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely, I was completing it as you commented :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.