0

任何解决方案都值得赞赏,不依赖于这种方法。

foreach(var x in xs){
    var y = getValuesfromX(x);

    foreach(var yvalue in y){

        //Here I want if(yvalue is LESS than 100 and if (yvalue - previousyvalue) not   GEATER than 30){

       // perform action

        //else, quit looping xs am not interested anymore after the difference reached 30
4

1 回答 1

2

There are approaches you could take using the Zip operator, but otherwise it's probably simplest to just keep the previous value - or a limit for the next value - as a variable:

foreach (var x in xs)
{
    var ys = GetValuesForX(x);
    int limit = int.MaxValue; // Any value is fine to start with.
    foreach (var y in ys)
    {
        if (y > limit)
        {
            break;
        }
        if (y < 100)
        {
            // Take some action 
        }
        limit = y + 30;
    }
}

Note that this will cause problems if y can ever be int.MaxValue - 30.

于 2013-06-27T06:25:12.293 回答