gpt4 book ai didi

c# - 执行 IEnumerable 方法

转载 作者:行者123 更新时间:2023-11-30 13:38:37 24 4
gpt4 key购买 nike

将问题陈述简化为 List of int,假设我有这个

 List<int> range = new List<int>(10) { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

用户输入 24,我需要 Early feed 值为 20,late feed 值为 30
用户输入 99,我需要 Early feed 值为 90,late feed 值为 100
用户输入 20,我需要 Early feed 值为 20,late feed 值为 20

是否有 Lamba 表达式或 linq 语句来获得这种结果。

我真正的输入不是 List of int,我只是简化了它,更热衷于 lamba 或 linq 表达式来得到结果

最佳答案

怎么样

range.Where(i => i <= UserInput).Max();

range.Where(i => i >= UserInput).Min();

或者替代

range.Max(i => i <= UserInput);
range.Min(i => i >= UserInput);

关于c# - 执行 IEnumerable 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15786065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com