4

我有许多Action具有属性的对象long Timestamp。我想做这样的事情:

Assert.IsTrue(a1.Timestamp < a2.Timestamp < a3.Timestamp < ... < an.Timestamp);

不幸的是,这种语法是非法的。是否有内置方式或扩展\LINQ\whatever 方式来执行此操作?

请注意,它是单元测试类的目标,所以要发疯。我不关心性能,可读性等。

4

4 回答 4

6
private static bool isValid(params Action[] actions)
{
  for (int i = 1; i < actions.Length; i++)
    if (actions[i-1].TimeStamp >= actions[i].TimeStamp)
      return false;
  return true;
}

Assert.IsTrue(isValid(a1,a2,...,an));
于 2011-02-07T14:49:39.330 回答
4

怎么样:

Action[] actions = { a1, a2, a3, ... an };
Assert.IsTrue
  (actions.Skip(1)
          .Zip(action, (next, prev) => prev.Timestamp < next.Timestamp)
          .All(b => b));
于 2011-02-07T14:50:55.107 回答
1

通过假设actions是一个列表或数组:

actions.Skip(1).Where((x,index)=>x.Timespan > actions[i].Timespan).All(x=>x)
于 2011-02-07T15:00:39.280 回答
1
public bool InOrder(params long[] data)
{
  bool output = true;

  for (int i = 0; i <= data.Count-1;i++)
  {
    output &= data[i] < data[i + 1];
  }
  return output;
}

我使用了 for 循环,因为这保证了迭代的顺序,而 foreach 循环不会这样做。

于 2011-02-07T14:49:12.623 回答