Ops Ops - 3 months ago 25
C# Question

Download data reverse Loop c#

I wanna download my e-mail's from Maibox only 100. But i'am trying my loop download data from lower to upper. I would like to download data from uper to lower

enter image description here

// for (int i = count; i >=0 && i <= 100; i--) /// <--- It works but if i have e-mails more than 100 not working.
for (int i = 1; i <= count && i <= 100 ; i++) // <-- this download from old data to new.

OpenPop.Mime.Message message = client.GetMessage(i);
OpenPop.Mime.MessagePart html = message.FindFirstHtmlVersion();

if (html != null)
htmlContained = html.GetBodyAsText();


This is your comment

// for (int i = count; i >=0 && i <= 100; i--) /// <--- It works but if i have e-mails more than 100 not working.

Now let me help you with explaining your own code. i = with the value that count contains

This loop will run while i greater then or equal to 0 AND less or equal to hard coded 100; i will be lower with 1 in every looping cycle.

Now, this 100 part. Here you restrict yourself to not see more then 100. If you remove it, you won't restrict yourself from seeing only the ones in the lower 100 end.

So just write

for (int i = count; i >=0; i--)

if you need the 100 limit just use your old query.

if you need it in reverse order just negate the expression like

for (int i = 0; i < count; i++)

the same with the 100 limit:

int limit = 100; for (int i = 0; i < limit; i++)

Also if you need a specific order just use linq query expressions.

Because I do not know your code I just make it up so you can ajust it or use google and msdn to adjust it to your needs.

var newEmailList = emails.OrderBy(email.whateverYouNeed).Take(100) or var newEmailListDesc = emails.OrderByDescending(email.whateverYouNeed).Take(100)

Then you can use a foreach to quickly handle you task.