LINQ to find the closest number that is greater / less than an input

后端 未结 8 621
猫巷女王i
猫巷女王i 2021-02-05 19:57

Suppose I have this number list:

List = new List(){3,5,8,11,12,13,14,21}

Suppose that I want to get the closest number th

8条回答
  •  攒了一身酷
    2021-02-05 20:37

    You can use a query for this such as:

    List numbers = new List() { 3, 5, 8, 11, 12, 13, 14, 21 };
    List output = (from n in numbers
                                where n > 13 // or whatever
                                orderby n ascending //or descending
                                select n).ToList();
    

提交回复
热议问题