Home > Blockchain >  How to update a list only after a foreach reaches a certain value?
How to update a list only after a foreach reaches a certain value?

Time:09-14

If i have a list with four columns -

  1. string ProcessSteps
  2. bool IsNext
  3. bool ReachedCurrentPosition
  4. char IsTransacted

say there is 10 records - And I am looking for the value IsNext = true and that is on the 5th record in the list... i want to be able to change each record ReachedCurrentPosition to false, after that record. How would i achieve that? is linq feasable? Basically, i want to output a button, until at this point, which i then need to update the proceeding records (ReachedCurrentPosition = true), to not allow them to output a button.

@foreach (var steps in ProcessSteps)
{
    <GridColumns>


        <GridColumn Field=@nameof(steps.IsNext) HeaderText="Set Next" TextAlign="TextAlign.Left" Width="110">


            <Template>

                @{
                    var step = context as ProcessStepModel;

                }


                @if (step.IsNext)
                {
                    <i>Current Position</i>

                    step.ReachedCurrentPosition = true;
                }

                @if (step.IsTransacted == 'Y' && step.ReachedCurrentPosition != true)
                {
                    <SfButton OnClick="@(args => OpenDialog(step.Id))" IsToggle="true" IsPrimary="true">Move PCB to this step</SfButton>
                }
            </Template>

        </GridColumn>
        <GridColumn Field=@nameof(steps.ProcessName) HeaderText="Name" Width="110"></GridColumn>
        <GridColumn Field=@nameof(steps.BenchName) HeaderText="Bench" Width="110"></GridColumn>
        <GridColumn Field=@nameof(steps.IsTransacted) HeaderText="Transacted" Width="110"></GridColumn>

    </GridColumns>
}

CodePudding user response:

Not sure if it's that's exactly what you're looking for, but you could do it like this

var steps = new List<Step>()
{
  new Step(false,false),  
  new Step(false,false),  
  new Step(false,false),  
  new Step(false,false),  
  new Step(true,false),  
  new Step(false,false),  
  new Step(false,false),  
  new Step(false,false),  
  new Step(false,false),  
  new Step(false,false),  
};

// find the item you want use as a starting point from where to update
var visited = steps.First(x => x.IsNext);
var index = steps.IndexOf(visited);

// take all the steps after the found item and update reached to true
var newSteps = steps.Skip(  index).ToList();  
newSteps.ForEach(s => s.ReachedCurrentPosition = true);

steps.ForEach(Console.WriteLine);

class Step
{
  public Step(bool isNext, bool reachedCurrentPosition)
  {
    IsNext = isNext;
    ReachedCurrentPosition = reachedCurrentPosition;
  }
  public bool IsNext { get; set; }
  public bool ReachedCurrentPosition { get; set; }

  public override string ToString() => $"IsNext: {IsNext}, Reached: {ReachedCurrentPosition}";
};

CodePudding user response:

Above the foreach add a bool something like this:

bool isNextReached = false;

Then inside your loop have this:

if(!isNextReached){
    //Do stuff with ReachedCurrentPosition being true
    if(IsNext == true){
        isNextReached = true;
    }
}
else{
    //Do stuff with ReachedCurrentPosition being false
}

This way the loop will behave differently depending on if IsNext has been found or not

  • Related