C# 获取System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]]而不是字符串
我正在从事一个MS Bot Framework项目,在该项目中,我正在从C中的数据库中的键值对中检索值。以前我有:C# 获取System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]]而不是字符串,c#,sql-server,botframework,keyvaluepair,C#,Sql Server,Botframework,Keyvaluepair,我正在从事一个MS Bot Framework项目,在该项目中,我正在从C中的数据库中的键值对中检索值。以前我有: var list = new List<KeyValuePair<int, string>>() { new KeyValuePair<int, string>(obj,_Obj.Questions) }; Dictionary<int, string> d = new Dictionary<int, string&
var list = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(obj,_Obj.Questions)
};
Dictionary<int, string> d = new Dictionary<int, string>
{
{ 1, "Welcome, How are you?" },
{ 2, "Tell me something about yourself."},
{ 3, "How much experience do you have?"},
};
我的目标是从数据库中获取诸如欢迎、你好吗、自我介绍等价值观。为了实现这一目标,我做了以下工作:
编辑:
问题1.cs
上述函数的值在该方法中得到:
MyDialog.cs//这是机器人程序的对话框
private static async Task<DialogTurnResult> **NameStepAsync**(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text(questions.AskName) }, cancellationToken);
}
我在GetChatsId方法中将Id值作为1传递。基于此,我应该得到相应的值
在NameStepAsync方法中,我接收到的是一个不寻常的父类,而不是我所期望的实际字符串:
System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]]
有人知道为什么会这样吗
谢谢。我无法正确理解您的问题,但您可以尝试下面的代码或共享更多信息
int idn=0;
foreach (DataRow row in dt.Rows)
{
string queMsg = row["Description"].ToString();
list.Add(new KeyValuePair<int, string>(idn, queMsg));
idn=idn+1;
}
您描述的输出是该方法的默认行为,即返回对象类型的完全限定名。由于List并没有覆盖ToString,所以您可以看到前面描述的输出 可通过以下方式复制:
Console.WriteLine(list.ToString()); // Or just: Console.WriteLine(list);
如果目的是输出列表中每个KeyValuePair的Value属性,我们需要首先从列表中的每个项目中选择该属性。例如:
var list = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string> (1, "Welcome, How are you?" ),
new KeyValuePair<int, string> (2, "Tell me something about yourself."),
new KeyValuePair<int, string> (3, "How much experience do you have?"),
};
list.ForEach(kvp => Console.WriteLine(kvp.Value));
我意识到这个答案忽略了您提供的大部分代码,但在我看来,您显示的代码并不是导致您描述的输出的原因。希望这能有所帮助 这回答了你的问题吗@贾瓦德,谢谢你的帮助。我在foreach循环中使用了dt.行。但是,在调试模式下,我得到的是上面的父类而不是实际的字符串。您在问题中输入的代码似乎并没有告诉我们字符串System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.string]]来自何处。看起来您正在有效地对列表实例执行.ToString操作。但我在代码中看不到这一点。您需要提供一个。@Enigmativity请查看我更新的问题。@RohanRao-您仍然没有提供代码的详细信息。例如,GetChats方法的结尾缺失。谢谢先生:请查看我更新的问题,我提供了更多详细信息。
var list = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string> (1, "Welcome, How are you?" ),
new KeyValuePair<int, string> (2, "Tell me something about yourself."),
new KeyValuePair<int, string> (3, "How much experience do you have?"),
};
list.ForEach(kvp => Console.WriteLine(kvp.Value));