0

I need to do this in my codes;

  1. Get data from 2 columns in a database (laborer and trx_date)
  2. Place the extracted data to a DataTable
  3. Explode the first column in the DataTable
  4. Place exploded_laborer and trx_date to an array with

    key->exploded_laborer
    exploded=>trx_date
    

I am able to get to number 3 I just need to do number 4. My code is below:

private void GetLocalData()
{
    const string sql = @"SELECT laborer, trx_date from tbl_jobs WHERE trx_date BETWEEN @fromDate AND @toDate";
    var laborerDataTable = new DataTable();
    using (var conn = new SqliteAccess().ConnectToSqlite())
    {
        using (var cmd = new SQLiteCommand(sql, conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@fromDate", dtpFrom.Value.ToString("yyyy-MM-dd"));
            cmd.Parameters.AddWithValue("@toDate", dtpTo.Value.ToString("yyyy-MM-dd"));
            laborerDataTable.Load(cmd.ExecuteReader());
        }
    }
    var exploded = new List<string>();
    foreach (DataRow row in laborerDataTable.Rows)
    {

        exploded.Add(row["laborer"].ToString().Split('|')[0]);
    }
}

Your help is very much appreciated.

2
  • What you meant by this: I am able to get to number 3 I just need to do number 4. My code is below: could you please be more clear? Commented Jan 13, 2017 at 4:09
  • I am able to do steps 1 - 3. I just don't know how to code the step 4. Commented Jan 13, 2017 at 5:02

4 Answers 4

2

I think you are supposed to create Dictionary<TKey,TValue> which represents a collection of keys and values. This might do the trick for you

laborerDataTable.AsEnumerable()
                .Select(row => laborerDataTable.Columns.Cast<DataColumn>()
                        .ToDictionary(column => row[laborer] as string
                                      column => row[trx_date] as string))

Thus the complete code might look like

private void GetLocalData()
{
    const string sql = @"SELECT laborer, trx_date from tbl_jobs WHERE trx_date BETWEEN @fromDate AND @toDate";
    var laborerDataTable = new DataTable();
    using (var conn = new SqliteAccess().ConnectToSqlite())
    {
        using (var cmd = new SQLiteCommand(sql, conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@fromDate", dtpFrom.Value.ToString("yyyy-MM-dd"));
            cmd.Parameters.AddWithValue("@toDate", dtpTo.Value.ToString("yyyy-MM-dd"));
            laborerDataTable.Load(cmd.ExecuteReader());
        }
    }
    var LabDict = laborerDataTable.AsEnumerable()
                .Select(row => laborerDataTable.Columns.Cast<DataColumn>()
                        .ToDictionary(column => row[laborer] as string
                                      column => row[trx_date] as string))
}

Edit

This is just to create a dummy table.

static DataTable GetTable()
{
    // Here we create a DataTable with four columns.
    DataTable table = new DataTable();
    table.Columns.Add("laborer", typeof(string));
    table.Columns.Add("trx_date", typeof(string));

    // Here we add five DataRows.
    table.Rows.Add("Indocin", "12/12/2010");
    table.Rows.Add("Enebrel", "12/1/2011");
    table.Rows.Add("Hydralazine", "1/12/2012");
    table.Rows.Add("Combivent", "11/12/2013");
    table.Rows.Add("Dilantin", "12/11/2014");

    return table;
}

normal core C# way

DataTable laborerDataTable = GetTable(); 
Dictionary<string, string> exploded = new Dictionary<string, string>();
foreach(DataRow row in laborerDataTable.Rows)
{
    exploded.Add(row.Field<string>(0), row.Field<string>(1));
}

Also, how do I print each of the row to a console?

foreach(var dct in exploded)
{
    Console.WriteLine(dct.Key + " Date is " + dct.Value);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Mohit, can you please show me in the normal core C# way. I do not know how to use linq yet. Also, how do I print each of the row to a console? Thank you.
what do you mean by normal core C# way should you want to use Dictionary or not? if yes than we can do that in the loop as well.
If this is LINQ then I wouldn't understand it please make it a simple loop. Thanks. But if this is not a LINQ then I guess this the only way to do it or if you can make it more understandable. Thanks.
Hey @Ibanez1408 I have updated the solution. please have a look if this piece of code is understandable or you need further clarification.
1

If you are expecting the result to a Key-Value pair then why an array? why not a Dictionary<string, string>? You can try something like this :

 Dictionary<string, string> laborerDict = laborerDataTable.AsEnumerable()
                                                          .ToDictionary(x => x.Field<string>("laborer"), 
                                                                        x => x.Field<string>("trx_date"));

Comments

0

Use in built HashMap in java refer the code below HashMap map = new HashMap<>(); Map.put(key,value); And to get data from map use Map.get(key) It has a lot of function, maybe you can refer them by searching Java API

Comments

0

when you want to have Key=>Value better you use Dictionary instead of List

var exploded = new Dictionary<string,string>();
foreach (DataRow row in laborerDataTable.Rows)
{
 exploded.Add(row["laborer"].ToString(),(row["trx_date"].ToString());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.