Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
hello guys..

im working on small utility to get names from database in sql select statements..

i have 4 fields to get the rows from db, but users may fill 1 or 2 or 3 or 4 fields..

so if the 4 fields are filled, the statement should ends with

SQL
WHERE a = field1 and b = field2 and c = field3 and d = field4


if they were 3 then the where statement follow the filled fields e.g a and c and d are filled then the statement should be

SQL
WHERE a = field1 and c = field3 and d = field4


all the above i've work on and done..

fields may vary between multiple values, i can use the "IN" statement, but the query takes too long time..

i've thought to split the "IN" statement into "=" statements and then join the results in one result..

my problem is how to do that!!
lets say that i have the following..
2d array that contains

a1,a2
b1
c1,c2
d1,d2,d3,d4

the result should be:

a1b1c1d1
a1b1c1d2
a1b1c1d3
a1b1c1d4
a1b1c2d1
a1b1c2d2
a1b1c2d3
a1b1c2d4
a2b1c1d1
a2b1c1d2
a2b1c1d3
a2b1c1d4
a2b1c2d1
a2b1c2d2
a2b1c2d3
a2b1c2d4

i think it is very easy, but i've spent 3 days working and i can't think anymore..
Posted
Comments
Amir Mahfoozi 23-Jan-12 1:15am    
Are you sure that this change will boost your speed ? Anyway, it can be built by using CTEs.

1 solution

It all depends on what do you want to do with the arrays when they are already created, how often, etc. The problem is: the "native" .NET arrays are not designed to add and remove elements dynamically after they are created. You can resize the array, but even this is best avoided as it is pretty expensive and hard to maintain. When you need some dynamic behavior, you need to use collections.

Now, let's approach 2D arrays. Let's review all your options from the simplest and least flexible, to the more flexible. Mind you, all the options are quite easy to use and implement.

  • 2D array declared as:
    C#
    string[,] array = new string[numberOfRows, numberOfColumns];
    As your example shows different number of elements in different rows, you can designate "unused" element as null.

    This is probably not the most adequate choice, but certainly very simple.
  • "Jagged array", or array of arrays of strings, declared as
    C#
    string[][] array = new string[numberOfRows][];
    With a jagged array, you can have arrays of different lengths uses as the elements of the outer array. Note that as this is an array if arrays, you can create the outer array once but have to create each inner array separately, in a loop, using new operator. For example:
    C#
    array[13] = new string[2] {"1", "2"}; //here, 2 is the number of some row


    You cannot insert or remove elements, but at any time, you can always replace the element of the outer array with a new instance of an inner array initialized with a different length.

    See also http://en.wikipedia.org/wiki/Jagged_array[^].
  • The list of the lists of string declared as:
    C#
    System.Collections.Generic.List<System.Collections.Generic.List<string>> list
        = new System.Collections.Generic.List<System.Collections.Generic.List<string>>();


    You need to create every internal list (let's say, representing a row of string, as per your example) separately with new operator; you can add/remove a string element in any inner list and add/remove an inner list as an element of outer list at any time.


Now you know what you can do. Decide what data structure to pick according to the set of operations you require.

—SA
 
Share this answer
 
v8
Comments
Espen Harlinn 22-Jan-12 15:48pm    
Good advice :)
Sergey Alexandrovich Kryukov 22-Jan-12 15:57pm    
Thank you, Espen.
--SA
CPallini 22-Jan-12 15:51pm    
Very good, 5++.
Sergey Alexandrovich Kryukov 22-Jan-12 15:57pm    
Thank you very much.
--SA
Malath Razooq 22-Jan-12 16:20pm    
well, thanks for your answer..

but I'm already have the code to store the values in 2D array, mine already have the exact previous example and I want to store the result as in the example in another array..
my question is how to get the "a1" concatenated with "b1" concatenated with "c1" concatenated with "d4" ..
then "a1" .. "d2"
..
then "a2" .. "d4"
if they are stored in 2D array in the above sequence

lets say
<pre lang="vb">sqls(0) = a1b1c1d1
sqls(1) = a1b1c1d2
sqls(2) = a1b1c1d3
sqls(3) = a1b1c1d4
sqls(3) = a1b1c1d4
..
sqls(i) = a2b1c2d4
</pre>
etc..

I think this drawing will clarify my question..
http://193.188.94.11/arrayhelp.jpg

thanks again..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900