【问题标题】:getting System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]] instead of string获取 System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]] 而不是字符串
【发布时间】:2020-01-08 04:46:44
【问题描述】:

我正在开发一个 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>
{
  { 1, "Welcome, How are you?" },
  { 2, "Tell me something about yourself."},
  { 3, "How much experience do you have?"},
};

我的目标是从数据库中获取诸如“欢迎光临,你好吗?”、“告诉我一些关于你自己的事情”等价值观。为了实现这一点,我这样做了:

编辑:

Questions.cs

public static string GetChats(int Id)
{


   using (SqlCommand cmd = new SqlCommand("usp_FetchData", con))
   {
   var list = new List<KeyValuePair<int, string>>();

   DataTable dt = new DataTable();
   cmd.CommandType = System.Data.CommandType.StoredProcedure;

   cmd.Parameters.AddWithValue("@id", Id);

   SqlDataAdapter da = new SqlDataAdapter(cmd);
   da.Fill(dt);

   con.Open();

   SqlDataReader reader = cmd.ExecuteReader();

   if (reader.HasRows)
   {
      foreach (DataRow row in dt.Rows)
      {
        string queMsg = row["Description"]?.ToString();
        list.Add(new KeyValuePair<int, string>(Id, queMsg));
      }
    }

 // class property

 public string WelcomeStmt = GetChats(1).ToString();

上面函数的值是在这个方法中获取的:

MyDialog.cs // 这是 Bot 的对话框

private static async Task<DialogTurnResult> **NameStepAsync**(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{

  return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = MessageFactory.Text(questions.AskName) }, cancellationToken);
}

我在 GetChats(Id) 方法中将 Id 值作为 1 传递。所以基于此,我应该得到对应的Value。

NameStepAsync 方法中,我收到了一个我猜想的不寻常的父类,而不是我期望的实际字符串。:

System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]]。

有人知道为什么会这样吗?

谢谢。

【问题讨论】:

  • @Jawad,谢谢这有点帮助。我在 foreach 循环中使用了 dt.Rows。但是,在调试模式下,我得到的是上面的父类而不是实际的字符串。
  • 您在问题中输入的代码似乎并未向我们显示字符串 System.Collections.Generic.List1[System.Collections.Generic.KeyValuePair2[System.Int32,System.String]] 的来源。看来您实际上是在 .ToString() 实例上执行 .ToString()。但我在代码中看不到这一点。您需要提供minimal reproducible example
  • @Enigmativity 请查看我更新的问题。
  • @RohanRao - 您还没有提供代码的minimal reproducible example。例如,GetChats 方法的结尾缺失。

标签: c# sql-server botframework keyvaluepair


【解决方案1】:

您描述的输出是Object.ToString 方法的默认行为,即返回对象类型的完全限定名称。由于List&lt;T&gt; 不会覆盖ToString,因此您会看到您所描述的输出。

可以通过以下方式复制:

Console.WriteLine(list.ToString());   // Or just: Console.WriteLine(list);

如果意图输出列表中每个KeyValuePairValue 属性,我们需要首先从列表中的每个项目中选择该属性。例如:

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));

我意识到这个答案忽略了您提供的大部分代码,但在我看来,您显示的代码并不是导致您描述的输出的原因。希望这会有所帮助。 :)

【讨论】:

  • 谢谢先生 :) 请查看我更新的问题,我提供了更多详细信息。
【解决方案2】:

我没有正确理解您的问题,但您可以尝试以下代码或分享更多信息

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2019-01-26
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多