Solved

Linking dictionary and columns together

Posted on 2016-10-17
2
70 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
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 35

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Read about achieving the basic levels of HRIS security in the workplace.
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…
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…

839 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