如何从C#数据库中检索KeyValuePair的值?

问题描述 投票:2回答:2

我正在从事一个涉及KeyValuePair的项目。我将int作为键,将字符串作为Value。

我想要的是我想从KeyValuePair获取值。

我有此代码:

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?"},
};

因此,如果您在KVP中看到字符串Values之类的字符串,例如“ Welcome,您好吗?”,“告诉我一些有关您自己的东西”等,则是静态的。所有这些值已经存在于我的本地数据库中。我只需要数据库中的这些字符串值。

我正在使用MS SQL数据库

这可能吗?

c# sql-server dictionary keyvaluepair
2个回答
0
投票

是,您的要求是可能的。

您必须建立数据库连接并运行查询以提取所需的数据。接收到数据后,将其放入集合中。

此集合可能包含KeyValuePair,就像您编写的一样,您可以创建一个类来复制表结构并包含这些对象的集合。

您还可以更进一步,研究诸如实体框架之类的东西。

祝你好运!


0
投票

您可以使用EntityFramework ToDictionary函数执行此映射。

var test = await this.dbcontext.InterviewQuestions
    .ToDictionaryAsync(x => x.Id, x.Description);
© www.soinside.com 2019 - 2024. All rights reserved.