1

I have this dictionary:

        Dictionary<char, ValuePair> dictionary = new Dictionary<char, ValuePair>();

With the following struct:

struct ValuePair
{
    public double Value1;
    public double Value2;
}

With the following values:

        dictionary.Add('A', new ValuePair { Value1 = 0.0, Value2 = 1.0 });
        dictionary.Add('B', new ValuePair { Value1 = 1.0, Value2 = 1.2 });
        dictionary.Add('C', new ValuePair { Value1 = 1.2, Value2 = 1.4 });
        dictionary.Add('D', new ValuePair { Value1 = 1.4, Value2 = 1.6 });

        dictionary.Add('E', new ValuePair { Value1 = 1.6, Value2 = 1.8 });
        dictionary.Add('F', new ValuePair { Value1 = 1.8, Value2 = 2.0 });
        dictionary.Add('G', new ValuePair { Value1 = 2.0, Value2 = double.MaxValue });

With query syntax I'm checking if a value is between one of the values, or if the case is G it justneed to be greater than 2.0. I don't like the solution with double.MaxValue. Is there any other way?

        var getValue = (from d in dictionary
                       where 2.1 >= d.Value.Value1 && 2.1 < d.Value.Value2 
                       select d.Key).FirstOrDefault();

        Console.WriteLine(getValue);

Thanks

4

1 回答 1

1

您可以使用可为空的值,请记住,任何类型的 null 和非 null 之间的比较都将返回 false。

如果您使 Value2 可以为空,您的条件将转换为:

2.1 >= d.Value.Value1 && !(2.1 >= d.Value.Value2)
于 2013-04-24T14:29:36.600 回答