如何使用foreach循环返回C#中的多个键值对?
目前,我已经编写了逻辑,以使用每个循环返回多个键值对,但它只是返回的第一个键值对。 我当前的代码是:
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;
}
}
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 quot;{{\"{item.Key}\":\"{item.Value}\"}}";
}
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 iterator block 返回项目列表
You can use an iterator block to return a list of items
使用
emumosit.Select.Select
< < << /a>扩展名:如果您仍然要重新使用此代码,则重新写
returndata
这样:请注意,
此
turn turnreturndata
进入扩展方法,该方法允许以这种方式调用:Use
Enumerable.Select
extension:If you still want to re-use this code, then re-write
ReturnData
like this:Note, that
this
turnsReturnData
into an extension method, which allows to call it this way: