text: Asked by:
  c# find number to group correlation
  Question
  All replies
  If you have a list of range for your groups, you can use a WHILE statement to move down at end of your list asking for if you number is between the actual range somethin like this.
  --Estructura lista de rango
  struct rango {
  id_group int;
  init_val int;
  end_val int;
  };
  List<rango> MyRanges() = new List()....
  int number = 23212;
  int grupo =0;
  while(grupo==0 && MyRanges!=end) {
  if(number>=MyRanges.init_val && number<=MyRanges.end_val)
  {
  grupo = MyRanges.Index;
  }
  }
  Something like that, of course some syntax is not good. I hope can help you.
  If you know the ranges are contiguous (no gaps), then all you have to store is the upper limit. You can stop the loop as soon as your number is below the upper limit of group N.
  using System; using System.Collections.Generic; class one { static public void Main() { int target = 32123; List<int> ranges = new List<int>(){10000,25000,37000}; for( var i = 0; i < ranges.Count; i++ ) { if( target <= ranges[i] ) { Console.WriteLine( "Found {0} in range {1}", target, i ); break; } } } }
  Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.
  Hi
  Thank you for posting here.
  Based on your description, you want to find the number to group correlation.
  I make a simple code, you could have a look.
  class Program { static void Main(string[] args) { List<int> list = new List<int>(); for (int i = 0; i < 25000; i++) { list.Add(i + 1); } var arrange1 = list.Where(x => x.IsBetween(1, 10000)).ToList(); var arrange2 = list.Where(x => x.IsBetween(10001, 25000)).ToList(); int m = 23212; List<List<int>> result = new List<List<int>>() {arrange1,arrange2 }; int count = 1; foreach (var item in result) { var test = item.Where(p => p == m).Count(); if (test > 0) { Console.WriteLine("m's value:{0} belongs to arrange{1}", m, count); } count++; } Console.ReadKey(); } } public static class Extensions { public static bool IsBetween(this int value, int min, int max) { return value >= min && value <= max; } }
  Result:.