简体   繁体   English

如何避免年龄范围重叠C#

[英]How to avoid age ranges overlapping C#

Can you help me to get a better way to verify or avoid that a new age range overlaps with an existing ones?? 您能否帮助我找到一种更好的方法来验证或避免新的年龄范围与现有年龄范围重叠?

I have a list of "age ranges" entity which have the minAge, maxAge properties and I want to add a new object of type age range to my List but it must not overlap the existing age ranges. 我有一个具有minAge,maxAge属性的“年龄范围”实体的列表,我想向我的列表中添加一个年龄范围类型的新对象,但是它不能与现有的年龄范围重叠。

List[0] (MinAge = 0, MaxAge = 2)
List[1] (MinAge = 3, MaxAge = 18)
List[2] (MinAge = 19, MaxAge = 54)

If I want to add Min = 10 and Max = 16 ... It must be avoided. 如果我想添加最小值= 10和最大值= 16 ...必须避免。 I need to add just the values that continue with the series. 我只需要添加该系列的后续值。

I was trying to validate each by each index from the list min and max between the ranges but I want to know if there is another way to do this with linq or something similar 我试图通过列表中范围之间的最小值和最大值来验证每个索引,但是我想知道是否有另一种方法可以使用linq或类似方法

Example: 例:

foreach(Age age in list)
{
     if(age.min >= min && max <= age.max)
     {
          return false;
     }
}
.Any(i => i.max >= min && i.min <= max);

如果有重叠,它将返回True

return !list.Any(x => x.min <= min && x.max >= min || x.min <= max && x.max >= max);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM