Solved

Linking dictionary and columns together

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

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

Suggested Solutions

Title # Comments Views Activity
SQL SERVER 2008 R2 Problem copying database 10 69
User Prompt in Vertica SQL (vsql) 1 36
Datagridview column resizing 8 26
Export individual report to pdf 2 38
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…

734 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