【问题标题】:How to populate dictionary using SQL table data如何使用 SQL 表数据填充字典
【发布时间】:2019-01-05 10:18:20
【问题描述】:

在我的 C# 项目中,我有一个字典,它的值当前是静态的。

var dict = new Dictionary<string, string>
{
    ["0000"] = "UK",
    ["1111"] = "JAPAN",
    ["2222"] = "CHINA",
    ["3333"] = "SRI LANKA",
    ["4444"] = "AUSI",
    ["5555"] = "USA",
};

现在我需要动态设置它的值。我怎样才能做到这一点?我有一个名为“country”的 Oracle 数据库表,其中有两个名为 IDCountryName 的列,如下所示,

ID  CID     CountryName

1   0000    UK
2   1111    JAPAN
3   2222    CHINA
4   3333    SRI LANKA
5   4444    AUSI
6   5555    USA

如何将此表值设置到我的字典中?提前致谢。

【问题讨论】:

    标签: c# sql oracle dictionary


    【解决方案1】:

    使用 oracle dataadapter 将数据放入 Datatable(请参阅https://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracledataadapter(v=vs.110).aspx)。然后使用下面的代码。您不需要像我一样将数据放入数据表中。刚刚将数据添加到表中以测试创建字典的代码。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable  dt = new DataTable();
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("CID", typeof(string));
                dt.Columns.Add("CountryName", typeof(string));
    
                dt.Rows.Add(new object[] {1, "0000", "UK"});
                dt.Rows.Add(new object[] {2, "1111", "JAPAN"});
                dt.Rows.Add(new object[] {3, "2222", "CHINA"});
                dt.Rows.Add(new object[] {4, "3333", "SRI LANKA"});
                dt.Rows.Add(new object[] {5, "4444", "AUSI"});
                dt.Rows.Add(new object[] {6, "5555", "USA"});
    
                Dictionary<string, string> dict = dt.AsEnumerable()
                    .GroupBy(x => x.Field<string>("CID"), y => y.Field<string>("CountryName"))
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());
            }
        }
    }
    

    【讨论】:

    • 提前致谢。我会尽力让你知道先生
    【解决方案2】:

    如果您有 Oracle 12.2,则可以使用 JSON_ARRAY 执行返回 json 字符串的查询:

    select JSON_ARRAY (cid, countryname) from country
    

    如果您的数据是 json 格式,使用 Json.Net 很容易将其转换为字典:

    string json = @"{""0000"":""UK"",""1111"":""Japan""}";
    
    var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
    

    【讨论】:

    • 提前致谢。我会尽力让你知道先生
    【解决方案3】:

    这里我使用了ToDictionary扩展方法,用于将IEnumerable集合转换成一个新的字典。

    public static void Main()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("CID", typeof(string));
        dt.Columns.Add("CountryName", typeof(string));
    
        dt.Rows.Add(new object[] { 1, "0000", "UK" });
        dt.Rows.Add(new object[] { 2, "1111", "JAPAN" });
        dt.Rows.Add(new object[] { 3, "2222", "CHINA" });
        dt.Rows.Add(new object[] { 4, "3333", "SRI LANKA" });
        dt.Rows.Add(new object[] { 5, "4444", "AUSI" });
        dt.Rows.Add(new object[] { 6, "5555", "USA" });
    
        Dictionary<string, string> dict = GetDictionary(dt);
    }
    
    public static Dictionary<string, string> GetDictionary(DataTable dt)
    {
        return dt.AsEnumerable()
          .ToDictionary<DataRow, string, string>(row => row.Field<string>(1),
                                    row => row.Field<string>(2));
    }
    

    Dotnet 小提琴:https://dotnetfiddle.net/KNs9Xl

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多