Solved

Linking dictionary and columns together

Posted on 2016-10-17
2
77 Views
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
            DataSheet.Add(eachLine);

        }
        //save the changes 
        db.SaveChanges();
        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
0
Comment
Question by:help needed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
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
1
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 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

1

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

690 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