Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
//for loop on all serial no.
foreach (var item in usersGroupSrNo.ToList())
{
    //group by date of one serno
    var obj1 = item.GroupBy(x => x.timestamp.Date).ToList();

    //finding decreasing bettary level upto lowest so 4 variable for computing uses

    decimal? firstBatteryLevel = -1;
    DateTime? firstTimeStamp = null;
    decimal? lastBatteryLevel = -1;
    DateTime? lastTimeStamp = null;

    // ---------------------------------
    var orderedItems = item.OrderBy(x => x.timestamp).ToList();
    firstBatteryLevel = orderedItems[0].batteryLevel;
    firstTimeStamp = orderedItems[0].timestamp;
    for (var i = 1; i < orderedItems.Count(); i++)
    {
        if (orderedItems[i].batteryLevel > orderedItems[i - 1].batteryLevel)
        break;
        lastBatteryLevel = orderedItems[i].batteryLevel;
        lastTimeStamp = orderedItems[i].timestamp;
    }
    if(lastBatteryLevel == -1)
    {
        result.Add(new clsReturn()
        {
            srno = orderedItems[0].serialNumber,
            dailyavg = "",
            sstatus = "Unknown"
        });
        continue;
    }


What I have tried:

Looking to convert the loop into recursive function as it should need to continue for rest of the records instead of break.
Posted
Updated 9-Jun-20 21:55pm
v2
Comments
Afzaal Ahmad Zeeshan 9-Jun-20 20:32pm    
A recursive approach might be prone to stack overflow exceptions, where a loop and iteration based approach (if it works!) is better from runtime's standpoint.

Their algorithmic complexity (more or less) is the same.
Member 13566383 10-Jun-20 7:08am    
There are several loops in the code you have posted.
Only one of the loops contains a break statement. When this statement is executed your outer loop (foreach....) will not terminate. It only terminates the loop directly enclosing this statement. So I think that's what you want your code to do.
You may rewrite your inner loop by using a while-statement if you don't want to terminate your loop with break;
I don't think that converting your code into recursively called method is good alternative.
By the way: The last visible statement in your code fragment ("continue;") may be unnecessary. But we can know about that only if we can see where the outer loop will be closed.
PIEBALDconsult 10-Jun-20 10:18am    
Yeah, do _not_ go recursive on that.

1 solution

Quote:
Looking to convert the loop into recursive function as it should need to continue for rest of the records instead of break.

Rewriting the function as recursive will not change its behavior. What you want is changing the behavior of the function, the function is your design, should not be difficult to change the design.

Note that we have no idea of what is supposed to do your code or how you want change its behavior, just remove the break.
Nobody but you can fix your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900