Aytac Ozkan

Aytac Ozkan

  • NA
  • 1
  • 820

Adding new element to dictionary in Linq

Mar 27 2015 11:26 AM

I would like to add new item to on my dictionary in my code,

public PvmPivotResponse Report_GetGmrToTypeOfIPP(int ProjectPeriodId, int ResearchGroupId) { PvmPivotResponse retVal = new PvmPivotResponse(); var nodes = GetSalesHierarchyData(); var flatNodes = nodes.FlattenTree(d => d.Id, d => d.ParentId); var results = flatNodes.Select( list => list .Select((d, i) => new KeyValuePair<int, FlatData>(i, d)) .ToDictionary(pair => string.Format("level{0}", pair.Key + 1),                      pair => pair.Value.Text)) .ToList();         retVal.Data = new List<object>{ JsonConvert.SerializeObject(new { data = results }, Formatting.Indented) }; return retVal; }

This my Json output, but I need to add new item to my dictionary it's called 'value'

 { "data": [ { "level1": "XXXXXX", "level2": "XXXXXX", "level3": "XXXXXX", "level4": "XXXXXX " }, ] }

then my new output should be like this,

{ "data": [ { "level1": "XXXXXX", "level2": "XXXXXX", "level3": "XXXXXX", "level4": "XXXXXX ", "val": "2" }, ] }

so how can I add this ('value') new item on my dictionary??

After the answers I have decided to give more details for a clear understanding, Well, I have got hierarchic data then for fetch them I have used RecursiveExtensions, please take a look [here] so While I have using this methods, I fetched some data from dbContext, like this.

ublic IEnumerable<FlatData> GetSalesHierarchyData() { IEnumerable<FlatData> _salesHieararchy = db.SalesHierarchy .Select(s => new FlatData() { Id = s.Id, ParentId = s.ParentId ?? 0, Text = s.Name, Val = 2 //This has been added after, normally it doesn't exist before.   }).ToList(); return _salesHieararchy; }