Currently I have written a logic to return multiple key value pairs using for each loop, but it's return just the first key-value pair. My current code is:
public static string ReturnData(IEnumerable<KeyValuePair<string, string>> abc)
{
if (abc != null)
{
foreach (KeyValuePair<string, string> item in abc)
{
return $"{{\"{item.Key}\":\"{item.Value}\"}}";
}
return null;
}
}
IEnumerable<string>and add theyieldkeyword beforereturninside theforeachloopabcdirectly wherever you use itvar dict = new Dictionary<string, string>(){ ["a"] = "a" };and you're callingforeach(string s in ReturnData(dict))- just doforeach(string s in dict.Select(kvp => $"{{\"{item.Key}\":\"{item.Value}\"}}"))