none
Get the indexes of a array RRS feed

  • Question

  • I did google for a solution but there where non that did what I want.

    I created  an array of strings and this serves as a index for an other array, I need the two index numbers of the location of two strings. The array of looks like:

    DataSet ds = AccessDbLoader.LoadFromFile(DBFile);
    foreach (object Column in ds.Tables)
    Columns.Add(Column);
    So, the data might be:

    [0]Container1 [0]bike [1]scooter [2]hammer [3]cup (and so on)

    [1]Can [0]beens [1]water [2]salt [3]potaoes (and so on)

    So in pseudocode I would like to program: Columns.Indexof[Can], Indexof[Salt] and the return should be 1,2 or Container1, Cup should return 0,3

    Any help would be appriciated,

    Regards,

    Rob.




    • Edited by AnotherRob Wednesday, November 14, 2018 10:59 PM
    Wednesday, November 14, 2018 10:54 PM

Answers

  • I accidentaly deleted my last post but the solution was to use dictionaries.

     Dictionary<string, Dictionary<string, int>> IndexB = new Dictionary<string, Dictionary<string, int>>();

    int TableCount = 0; foreach (DataTable value1 in ds.Tables) { Dictionary<string, int> IndexS = new Dictionary<string, int>(); { for (int z = 0; z < value1.Columns.Count; z++) { IndexS.Add(value1.Columns[z].ColumnName, z); } IndexB.Add(ds.Tables[TableCount].TableName, IndexS); } TableCount = TableCount + 1; }




    • Marked as answer by AnotherRob Saturday, November 17, 2018 10:46 PM
    • Edited by AnotherRob Saturday, November 17, 2018 10:58 PM
    Saturday, November 17, 2018 10:44 PM

All replies

  • Hi,

    you can use Colums.ToList().IndexOf("Can");

                string[] Colums = new string[] { "beens", "Can" };
                var index = Colums.ToList().IndexOf("Can");

    Greetings, Chris


    • Edited by DerChris88 Wednesday, November 14, 2018 11:20 PM
    Wednesday, November 14, 2018 11:18 PM
  • Columns.Add(Column);

    Are you sure it's an array and not a list?

    var index = Colums.IndexOf("Can");
    Wednesday, November 14, 2018 11:28 PM
  • Hi AnotherRob,

    Thank you for posting here.

    For your question, if you want to get the two index numbers of the location of two strings, please try the code below.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace test1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strcon = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Exersize;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                SqlConnection sqlConnection = new SqlConnection(strcon);
                sqlConnection.Open();
                string sql = "select* from test1";
                SqlDataAdapter sda = new SqlDataAdapter(sql, sqlConnection);
                DataSet ds = new DataSet();
                sda.Fill(ds, "test1");
                DataTable table = ds.Tables["test1"];
                DataRow[] row = table.Select();
                for (int i = 0; i < row.Length; i++)
                {
                    for(int j=0;j<table.Columns.Count;j++)
                    {
                        if (row[i][j].ToString() == "Can")
                        {
                            Console.WriteLine(j);
                        }
                        else if(row[i][j].ToString() == "salt")
                        {
                            Console.WriteLine(j);
                        }
                        else if (row[i][j].ToString() == "Container1")
                        {
                            Console.WriteLine(j);
                        }
                        else if (row[i][j].ToString() == "cup")
                        {
                            Console.WriteLine(j);
                        }
                    }
                }
                Console.ReadKey();
            }
        }
    }
    

    Database:

    Result:

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 15, 2018 7:21 AM
    Moderator
  • Chris,

    You are right, it is a arraylist

    ArrayList Columns = new ArrayList();
    DBFile = openFileDialog1.FileName;
    DirectoryPath = Path.GetDirectoryName(DBFile);
    DataSet ds = AccessDbLoader.LoadFromFile(DBFile);
    foreach (object Column in ds.Tables)
       Columns.Add(Column);
    This loads the column names of all the tables of the database in the arraylist "Columns" as a index. The database is in an other array list. From here I need the numbers from the Index Columns to get the data in the arraylist containing the real data.
    Thursday, November 15, 2018 5:02 PM
  • Hi AnotherRob,

    try this:

                var index = Columns.IndexOf("Can");
                var item = anotherArrayList[index];

    Greetings, Chris

    Thursday, November 15, 2018 5:10 PM
  • I accidentaly deleted my last post but the solution was to use dictionaries.

     Dictionary<string, Dictionary<string, int>> IndexB = new Dictionary<string, Dictionary<string, int>>();

    int TableCount = 0; foreach (DataTable value1 in ds.Tables) { Dictionary<string, int> IndexS = new Dictionary<string, int>(); { for (int z = 0; z < value1.Columns.Count; z++) { IndexS.Add(value1.Columns[z].ColumnName, z); } IndexB.Add(ds.Tables[TableCount].TableName, IndexS); } TableCount = TableCount + 1; }




    • Marked as answer by AnotherRob Saturday, November 17, 2018 10:46 PM
    • Edited by AnotherRob Saturday, November 17, 2018 10:58 PM
    Saturday, November 17, 2018 10:44 PM