protected bool CheckBase(List datas, int index, int count, int threshold, Func action, int offset = 10) where T : struct, IComparable { var range = datas.GetRange(index, count); var res = range.All(x => x.CompareTo((T)(dynamic)(threshold + offset)) <= 0 && x.CompareTo((T)(dynamic)(threshold - offset)) >= 0); return action(res); } protected bool CheckBase (List datas, int index, int count, List thresholds, Func action, int offset = 10) where T : struct, IComparable { var range = datas.GetRange(index, count); var res = range.Zip(thresholds, (data, threshold) => data.CompareTo((T)(dynamic)(threshold + offset)) <= 0 && data.CompareTo((T)(dynamic)(threshold - offset)) >= 0 ).All(x => x); return action(res); }
定义了两个泛型方法 CheckBase
第一个方法 CheckBase
该方法首先通过 datas.GetRange(index, count) 从传入的 datas 列表中获取指定索引和数量的子列表 range。然后使用 LINQ 方法 All() 遍历 range 中的每个元素 x,检查其是否在阈值范围内,即是否满足 (x.CompareTo((T)(dynamic)(threshold + offset)) <= 0 && x.CompareTo((T)(dynamic)(threshold - offset)) >= 0) 的条件。最后,将结果传递给传入的 action 委托进行处理,并返回结果。
第二个方法 CheckBase
这两个方法可以用于校验给定数据列表中的数据是否满足指定的阈值要求,并通过提供的委托进行结果处理。
示例
// 示例 1 - 使用 CheckBase(List datas, int index, int count, int threshold, Func action, int offset = 10) 方法 List dataList = new List { 5, 7, 9, 11, 13, 15 }; int startIndex = 0; int elementsCount = 4; int threshold = 10; int offset = 2; bool result1 = CheckBase(dataList, startIndex, elementsCount, threshold, (res) => { Console.WriteLine($"All elements in the range [{startIndex}-{startIndex + elementsCount - 1}] are within the threshold +/-{offset}: {res}"); return res; }, offset); // 输出: All elements in the range [0-3] are within the threshold +/-2: False // 示例 2 - 使用 CheckBase (List datas, int index, int count, List thresholds, Func action, int offset = 10) 方法 List dataValues = new List { 1.2, 1.4, 1.5, 1.8, 2.1 }; int startIndex2 = 1; int elementsCount2 = 3; List thresholds = new List { 1, 2, 3 }; int offset2 = 0; bool result2 = CheckBase(dataValues, startIndex2, elementsCount2, thresholds, (res) => { Console.WriteLine($"All elements in the range [{startIndex2}-{startIndex2 + elementsCount2 - 1}] are within the thresholds +/-{offset2}: {res}"); return res; }, offset2); // 输出: All elements in the range [1-3] are within the thresholds +/-0: False
猜你喜欢
- 3天前(兰州旅游文化产业发展有限公司)甘肃省兰州市2023年乡村旅游暨A级旅游景区管理工作培训班开班
- 3天前(郭富城热舞劲歌演唱会)郭富城年度压轴《新濠尊属系列郭富城梦幻舞林演唱会2023》
- 3天前(安徽民宿发展报告)首届安徽省乡村民宿创意设计大赛启动
- 3天前(七尚酒店百度百科)Lohkah七尚酒店首度开创充满新知的闽地研学旅程
- 3天前(甘州区文化旅游局)2025甘津文旅资源对接推介会在兰州举办
- 3天前(花王伴你乐享五一好“趣”处)花王伴你乐享五一好“趣”处
- 3天前(澳涞坞是什么)从最美山庄到世界舞台:澳涞山庄见证世界十佳旅居城市评选
- 3天前(当科学邂逅喜剧:科技馆喜剧嘉年华背后的"文旅破壁者")当科学邂逅喜剧:科技馆喜剧嘉年华背后的"文旅破壁者"
- 3天前(新西兰旅游局×时差岛:黄觉的“长白云之乡”奇遇)新西兰旅游局×时差岛:黄觉的“长白云之乡”奇遇
- 3天前(芜宣机场国际航班)新华丝路:芜宣机场开通至越南首都河内的国际货运航线
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章