2

I really have no clue about enumerated list, but after some research I found that this list may help solve my problem. So I have a string in my settings called strGrades, and it is a range of strings that I manually update. The range is 0155-0160, 0271-0388, 0455-0503, 0588-687. What I basically want to do is find the values that are not in this grade list (for example 0161,0389, 0504-0587...)

So I came up with a function that will allow me to get each match in the grade range:

public static List<GradeRange> GetValidGrades()
{
    MatchCollection matches= Regex.Matches(Settings.Default.productRange,
                                           Settings.Default.srGradeRange);

    List<GradeRange> ranges= new List<GradeRange();

    if(matches.Count >0)
    {
        foreach (Match match in matches)
        {
            ranges.Add(new GradeRange() 23 {
                       Start= int.Parse(match.Groups["Start"].Value),
                       Stop= int.Parse(match.Groups["Stop"].Value)
            });    
         }

     }
     return ranges;
}

here is the grade range class

public class GrandRange
{
    public int Start{get; set;)
    public int Stop {get; set; )
}

So the function above caputures my Start and End values, can anyone please help me get this into a list where I can find the values that fall outside of the range values, I just need a starting point. Thanks so much!

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Naina
  • 77
  • 7
  • You could use `Contains()` and roll your own `IEqualityComparer` to perform the check for you. – Rory McCrossan May 03 '12 at 14:46
  • how did you get 0161,0389,0504-0587 from the ranges before? 0161 is just after 0161, but why is 0162 not included in your expected set of ranges? Why are the range start numbers prefixed with a zero? Just a formatting thing? – vidstige May 03 '12 at 15:11

2 Answers2

3

You could use a custom extension method that creates .Between along with a Where

var myFilteredList = list.Where(x=>!myValue.Between(x.Start, x.Stop, true));

This isnt the most performant answer, but if you need a list of all the numbers that are not between certain ranges, then you could do something like this:

var missingNumbers = new List<int>();
var minStop = list.OrderBy(x=>x.Stop).Min().Stop;
var maxStart = list.OrderBy(x=>x.Start).Max().Start;
Enumerable.Range(minStop, maxStart).ToList()
    .ForEach(x=>
        {
            if(!x.Between(x.Start, x.Stop, true))
                missingNumbers.Add(x);
        }
    );
Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • 1
    I think this is not what OP wants. I as understand, (s)he wants to find the gaps between ranges, not to filter the list – Ortiga May 03 '12 at 14:51
  • @Andre Not quite as pretty or performant, but I updated my answer to figure out all of the gaps – Justin Pihony May 03 '12 at 15:26
  • I got the ranges from a preset calculation. Yes they are pre set with zero for formatting. So I will try this custom extension and post my results. Thanks – Naina May 03 '12 at 16:11
  • @ Justin Pihony, how did you get your answer to get the gaps in between? – Naina May 04 '12 at 03:02
  • @Naina I am not sure if I understand your question. What do you mean, how did I get the answer? Experience and a good google to verify? – Justin Pihony May 04 '12 at 04:31
  • @ Justin Pihony where did you get myValue from?? Just curious, your code is hard to follow. Can you explain how you got that, if you don't mind. – Naina May 05 '12 at 21:14
  • @Naina myValue would just be the value you are checking. That line just says something like this: from list select those values where the given value (myValue) is not between x.start and x.stop inclusive of the actual start and stop values – Justin Pihony May 05 '12 at 22:54
  • @ Justin Pihony can I show you what I have so far, I am not getting the values out of the between clause, and can you give me some guidance on where I am going wrong. I will post my code and show you. – Naina May 06 '12 at 00:09
0

Here this should get you started

        var strings = "0155-0160, 0271-0388, 0455-0503, 0588-687";
        var splitStrings = strings.Split(char.Parse(","));
        var grads = new List<GrandRange>();
        foreach (var item in splitStrings) { 
            var splitAgain = item.Split(char.Parse("-"));
            var grand = new GrandRange
            {
                Start = int.Parse(splitAgain[0]),
                Stop = int.Parse(splitAgain[1])
            };
            grads.Add(grand);
        }

    }
Sean Barlow
  • 588
  • 6
  • 11
  • Ok so I used your method to get the start and stop ints. Thanks, so now how do I count through the grads list by start and stop values? – Naina May 03 '12 at 16:32
  • I have got the way you get the start int and stop, now how do I put this range into an enumerable range :Enumerable.Range(Stop, Start).ToList()?? Where I put a counter to count after each Start and Stop int? This is where I am stuck, any help would be greatly appreciated. This is my first try with enumerable ranges, and I am trying to get a working example on how to use them. Thanks you guys for all your help! – Naina May 03 '12 at 16:44