C# 从 List 中删除 NaN

本文关键字:删除 NaN object List | 更新日期: 2023-09-27 18:33:48

我有一个类型为StackBuilder的列表:

public class StackBuilder
{
    private int quoteID = 0;
    private double price = 0;
    private double size = 0;
    public int QuoteID
    {
        get { return quoteID; }
    }
    public double Price
    {
        get { return price; }
    }
    public double Size
    {
        get { return size; }
    }
    public StackBuilder(int quoteID, double price, double size)
    {
        this.quoteID = quoteID;
        this.price = price;
        this.size = size;
    }
}

我正在尝试删除价格等于 NaN 的所有元素。这就是我所拥有的,但由于某种原因它不起作用。

BidStack.RemoveAll(item => item.Price == Double.NaN);
AskStack.RemoveAll(item => item.Price == Double.NaN);

有什么建议吗?

解决:

BidStack.RemoveAll(item => Double.IsNaN(item.Price));
AskStack.RemoveAll(item => Double.IsNaN(item.Price));

C# 从 List<object> 中删除 NaN

==运算符在NaN上不能按预期工作,而是使用 Double.IsNaN(x)

相关文章: