简体   繁体   中英

c# Convert Dictionary<string, List<>> to Dictionary<string, object>

how can I append string to Json output in c#(linq) way

// Dictionary<string, List<object>>
var res = results.Where(x=>x.ConfidenceScore>0)
                            .GroupBy(x=>x.PropertyName)
                            .ToDictionary(g=>g.Key, 
                                    g=> g.Select(x=>x.Value.ToString()).ToList()
                                    ); 

>>> ??? res["OcrText"] = doc.Text;
Console.Out.WriteLine(JsonConvert.SerializeObject(res));

currently I have to copy whole dictionary to another be element to cast types

Dictionary<string, object> ugly = new Dictionary<string, object>();

foreach (var item in res)
{
  ugly.Add(item.Key, item.Value);
}
ugly["OcrText"] = doc.Text;

If i understand you correctly and you just want to convert Dictionary<string, List<object>> to Dictionary<string, object>()

You could use ToDictionary

var result = dict.ToDictionary(x => x.Key, x => (object)x.Value);

However its debatable why you would want to do this

or maybe you want

var res = results.Where(x => x.ConfidenceScore > 0)
               .GroupBy(x => x.PropertyName)
               .ToDictionary(g => g.Key,
                  g => (object)g.Select(x => x.Value.ToString()).ToList()
               );

Though on saying that, i'm pretty confused by the question, and i think you need to clarify things more

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM