[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Linking dictionary and columns together

Posted on 2016-10-17
2
Medium Priority
?
83 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 45

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 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

1

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 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