linq - C# : Checking if an object (who has a min and max property) falls between any existing objects? Explained inside -
i wonder if can help?
i have list collection of objects, each object has min , max property.
so imagine have
id : 1 min : 10 max : 49 id : 2 min : 50 max : 69 id : 3 min : 70 max : 89
i should not allowed create new object so.
id : 4 min : 88 max : 91
as falls within range of have already. (it overlaps id 3) hence no overlapping ranges.
i did try solve so, isn't working
var test = myobjects.findall( o => myminval.value >= o.min && mymaxval.value <= o.max).tolist();
so if test not null, means have overlap.
i have tried reversing greater , less signs....
any ideas how work?
thanks in advance
edit
also take example object, should fail although doesn't clash specific 1 object! clashes when @ 3 original.
id : 4 min : 5 max : 91
i hope makes sense
you're not checking enough things.
basically need see if either min in range, or if max in range of objects. here's unit test w/ working lambda:
[testclass] public class overlap { list<testdata> data = new list<testdata> { new testdata { id = 1, min = 10, max= 49}, new testdata { id =2, min=50, max=69}, new testdata { id=3, min = 70, max = 89} }; [testmethod] public void boundarycheck() { var mydata = new testdata { id=4, min=69, max=100}; bool fail = data.any(d => (d.min <= mydata.min && d.max >= mydata.min) || (d.max >= mydata.max && d.min <= mydata.max)); assert.istrue(fail); } class testdata { public int id { get; set; } public int min { get; set; } public int max { get; set; } } }
Comments
Post a Comment