Reputation: 2227
I am trying to create a maintenance page where an admin could create/amend/delete age ranges which then show up on the page for a customer to select their age range.
Age Range
1-20,
21-40,
41-60,
61-80,
81-100
Allow the values to be amended and deleted but the range cannot overlap or exist in the first place
So based on the above the below entries CANNOT exist
2-30 - falls within a range or
22- 30 - falls within a range etc etc
So far i have some code to check if the range exists
IEnumerable<AgeRange> ar = from a in myDataContext.AgeRanges.Where(aa => aa.MinAge <= NewAge.MinAge && aa.MaxAge >= NewAge.MinAge) select a;
Not fully tested but so far seems to do the trick. So i added in a delete function. IF i delete the range 41-60 then i have a new set of issues.
If i add a new entry 42 - 59 then the customer cannot select any range if their age is 41 or 60 as the above code would not find the above range so it would allow it to save. So the amended range in this example would be
1-20
21-40 - Customer is 41 so they cant select this option
42-59 - Customer is 41 so they cant select this option either. If the customer is 60 cant select this option
61-80 - If the customer is 60 cant select this option either.
81-100
I think i would have similar issues when updating an existing range.
Is there an easy way to do this or an appropriate control that i could use or any other ideas?
Thanks
Upvotes: 0
Views: 691
Reputation: 1093
The technique that everyone has described so far is the simplest answer. Instead of having the admin enter "1-10,11-20,21-30" they simply enter "10,20,30". You write code that sorts all these maximum age numbers once the admin is finished and creates the entire list of age ranges dynamically. (range1 = 1 through max1, range2 =[max1 + 1 through max2...rangeN = max(N-1) + 1 through maxN)
If you absolutely must allow the user to enter both minimum and maximum ages, then you want to validate the entire set once they click save/finish/etc. To do this, I would write a method that sorts the entire set of ranges in ascending order and then test that each age range (after the first) has a minimum age equal to the maximum age of the range before it.
Upvotes: 0
Reputation: 7401
One possibility may be to have a single "Maximum Age" field. Your check for a range already existing becomes simpler:
IEnumerable<AgeRange> ar = from a in myDataContext.AgeRanges
.Where(aa => aa.MaximimAge != NewAge.MaximumAge) select a;
You also have no need to worry about leaving a gap between ranges (as that would be automatically filled).
The only issue you're left with would be within the UI, rendering your list of " min - max" values. However, it should be relatively simple to write a few lines of code that could, for any given AgeRange
, finds the maximum age of the "next lowest" AgeRange
, adds 1 to it and counts that as the minimum of the current one.
Upvotes: 2