获取一个介于from和limit之间的值

时间:2015-11-26 17:12:59

标签: c# linq

我有一份礼物清单。我传递了一个值,我想检查这个值是否与Gift列表中的一个(或多个中的第一个值)最匹配。

List<GiftConfig> m_Gifts = new List<GiftConfig>() { 
                new GiftConfig () { Id = 1, From = 5000000, To = 8999999},
                new GiftConfig () { Id = 2, From = 9000000, To = 18999999},
                new GiftConfig () { Id = 3, From = 1900000, To = 25000000},
                new GiftConfig () { Id = 4, From = 35000000, To = 0},
            };

Ex:7000000 =&gt; Id = 1 10000000 =&gt; Id = 2

2 个答案:

答案 0 :(得分:1)

这应该可以解决问题

public class SimpleRectangle {

    private double height;
    private double width;

    public SimpleRectangle() {
        this.height = 1;
        this.width = 1;
    }

    public double getArea() {
        return height * width;
    }

    public double getPerimeter() {
        return height + height + width + width;
    }


    public static void main(String[] args) {
        SimpleRectangle rectangle1 = new SimpleRectangle();
        System.out.println("The area of radius " + rectangle1.getPerimeter() + " is  " + rectangle1.getArea());
    }

}

答案 1 :(得分:0)

int value = 7000000;
GiftConfig gift = m_Gifts.FirstOrDefault(x => value >= x.From && value <= x.To);
// Returns Id 1
value = 10000000;
gift = m_Gifts.FirstOrDefault(x => value >= x.From && value <= x.To);
// Returns Id 2
value = 999999999;
gift = m_Gifts.FirstOrDefault(x => value >= x.From && value <= x.To);
// Returns null (assuming GiftConfig is a class).

这将返回第一个匹配的礼物,如果没有,则返回null - “最佳”有点模棱两可。还有点不确定你期望用最终记录做什么 - 为什么'到'0?这是一个特例吗?