Linking dictionary and columns together

Posted on 2016-10-17
Medium Priority
Last Modified: 2016-10-17
I would like to know how could I link values that are in a dictionary to columns that are in another table. Both of them are in the same database. Thank you. The software I am using is Microsoft visual studio.

These are my codes to link them :

 public bool linkTgt()
        //retrieve the columns in Claim Table
        var allData = db.Claim.ToList();
        // create the dictionary to be able to have a space to add each variable 
        var dic = new List<Dictionary<string, string>>();
        //to save each variable information
        Dictionary<string, string> eachLine = new Dictionary<string, string>();

        foreach (var expence in db.Expence)

            //to check if there is this key in the dictionary called dic
            var key = "name";
            if (eachLine.ContainsKey(key))
                string name = eachLine["name"];
                //if there is then the value of column in expence table is equal to the value in the dictionary
                expence.Name = name;

            //Add it back to the dictionary field

        //save the changes 
        return true;

Open in new window

Errors I get : Nothing changes when I change the data of the dictionary even though i had link them together.

Dictionary : [{"name":"Jamie","totalexpense":"13.09"}]

Columns : Name, Total Expense

Current output

Expected OutputExpected output
I would like to have the columns to be updated when the dictionary fields are changed
Question by:help needed
LVL 45

Expert Comment

ID: 41846397
>>I would like to have the columns to be updated when the dictionary fields are changed
You have to write the code to do that yourself
LVL 36

Accepted Solution

Miguel Oz earned 2000 total points
ID: 41846426
eachLine dictionary is empty, thus the reason no updates happened.
To fill up with data from claim
Dictionary<string, string> eachLine = allData.ToDictionary(x =>x. Name, x => x.TotalExpense);

Open in new window

The first lambda lets you pick the key, the second one picks the value.

Also replace line 16-20 with: (Assuming you only like to update expence)
foreach(KeyValuePair<string, string> entry in myDic)
    // do something with entry.Value or entry.Key
	if (expence.Name == entry.Key) //check for name
	  expence.TotalExpense = entry.Value;

Open in new window


Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Among the most obnoxious of Exchange errors is error 1216 – Attached Database Mismatch error of the Jet Database Engine. When faced with this error, users may have to suffer from mailbox inaccessibility and in worst situations, permanent data loss.
Blockchain technology enhances society similar to the Internet. Its effects are broad, disruptive, and will boost global productivity.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question