Question Doing a Binary Search and Returning a lookup


New member
May 29, 2014
Programming Experience
Hi I have a binary search method to search for a particular record. I would have an index location. Now i want to use this index location to fetch from the recordlist as group by using lookup and not Groupby:
    public static Data BinSearch(MyData searchDate)
            IComparer<Data> rc = new RecComparer();

            int index;
            index = recordList.BinarySearch(searchDate, (IComparer<MyData>)rc);
             int i = 1;
            foreach (var r in recordList)
                Console.WriteLine("Line:" + i + " " + r.DateDetails + " " + r.TrackDetails);
            if (index >= 0)
               // return recordList[index];

            index = ~index;

            if (index == 0 || index == recordList.Count)
              //  return null;

            int newIndex = (((index-1)+index)/2)+1;
            string pointer = recordList[newIndex].DateDetails; // My pointer comes out correctly. and points to the right index I need. Now using the below line of code I'm getting into groups by using lookup:
 var lookup = recordList.ToLookup(d => d.DateDetails); // Now is where the problem comes up if I write the following code:
    var test = lookup.ElementAt(pointer);
     foreach (var print in lookup.ElementAt(newIndex))
           {Console.WriteLine("{0} {1}", print.Data1, print.Data2);
return (print);

It's giving me an exception outOF range because initially the recordlist had 180+ records in the recordlist. Now with lookup it did a grouping and it became around 60 records in the test variable. So it's throwing an exception.
I hope you understood my problem.How do I get the last part to work, because I want to return as a Data type with lookup and not Igrouping type by groupby.
Please help. THank you
Top Bottom