0

I have an array built from a table which I need to search values in..

Private marrOutscomesStart(2, 100000)

    Dim i As Integer = 0

    If Not rstType.BOF And Not rstType.EOF Then
        While Not rstType.EOF
            If Not IsDBNull(rstType.Fields(0).Value) Then
                marrOutscomesStart(0, i) = rstType.Fields(0).Value
                marrOutscomesStart(1, i) = rstType.Fields(1).Value
                i = i + 1
            End If
            rstType.MoveNext()
        End While
    End If

The data in the array will be something like this:

0 -- [Apple], [1], [Y]
1 -- [Apple], [2], [N]
2 -- [Apple], [3], [Y]
3 -- [Pear], [1], [Y]
4 -- [Pear], [2], [N]
5 -- [Banana], [1], [Y]

I would like to search for Column 1 = Apple and Column 2 = 2 and get the index of the array item.. I can loop through the array but is there a faster way to search for 2 column values?

Many thanks,

Derek.

Derek Jee
  • 147
  • 1
  • 14
  • Unfortunatly, You cannot avoid looping the array to search – Keith Mifsud Aug 25 '16 at 08:40
  • Maybe there is a way using LINQ See [this](http://stackoverflow.com/questions/7332103/query-an-object-array-using-linq) and [this](http://stackoverflow.com/questions/13822750/how-to-use-linq-on-a-multidimensional-array-to-unwind-the-array) post – Alberto Aug 25 '16 at 08:46
  • You can use [DataSet tables](https://msdn.microsoft.com/es-es/library/system.data.dataset.tables(v=vs.110).aspx) and search for columns – Raskayu Aug 25 '16 at 08:51
  • what are the 2 array lists names and what datatypes are they? also show us what the values are that they are holding – user1234433222 Aug 25 '16 at 09:41
  • I don't think an array is the best data-type for storing this data. I'd store it in a data-set and then use a LINQ query. – Ryan Thomas Aug 25 '16 at 11:38
  • You should definitely look into list and classes. You initialize your array at 100000, if you are looping through the whole thing, even if most of the elements are empty. It'll be very slow. At least keep a variable for the max length of data. – the_lotus Aug 25 '16 at 11:44

1 Answers1

0

Hold on, hold on. Are you telling that you are extracting entire database table to find interesting records? Am I not getting something? Doesn't it provide some way of SQL query like:

SELECT FRUIT, VALUE, THIRD_COLUMN FROM TABLE WHERE FRUIT='Apple' AND VALUE='1';

If you really have to manually scan entire table: - If you are doing it once, it will be fasters to just scan it from top to bottom - If you are searching multiple times for different values, I would suggest (quick)sorting the table, then use binary search.

Edit:

I'm not sure what you are trying to achieve, but first of all storing it in two dimensional array is bad idea .First you should create class with Fruit and Number as fields and implementing IComparable - add method which will compare two objects and put it in some order. Then you can use Dictionary which will do sorting and finding for you.

monkeyman79
  • 602
  • 4
  • 13
  • Hi there, sorry I am actually filling 2 arrays which will be used in a comparison when a user clicks save on a winform. There are multiple checkboxes which can be clicked and I am using the arrays to see what has changed.. – Derek Jee Aug 25 '16 at 08:59
  • @DerekJee use [Observer pattern](http://stackoverflow.com/questions/10069299/observer-design-pattern) to check the changes, and save it in some data structure – Raskayu Aug 25 '16 at 09:14
  • Thank you MonkeyMan.. That is the way to go thank you.. I am a bit stiffled by legacy code but yours is the way I shall go.. Many thanks to you all!! – Derek Jee Aug 25 '16 at 12:20