如何创建通过定义键和每个条目的值在C#中的数组? [英] How to I create an array in C# by defining the key and the value of each entry?

查看:126
本文介绍了如何创建通过定义键和每个条目的值在C#中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个数组,我可以从数据库中读取数据,然后用2列创建数组。

要得到这个在PHP中完成的,这里是我做了

 私有函数_getSystemRules()
{
    $规则= $这个 - > DB-GT&; getDataSet(SELECT SQL_CACHE SECTION_NAME,privilege_key,privilege_value
                                    从user_privileges
                                    WHERE状态= 1
                                    ORDER BY SECTION_NAME');    的foreach($规则$规则){
        $这个 - > rolesList [$规则['SECTION_NAME'] [$规则['privilege_key'] = $规则['privilege_value'];
    }
}

更新时间:

最后阵列看起来像这样

 阵列

    [帐户] =>排列
        (
            [can_own_account] => 4
            [can_view_summary] => 64
            [can_update_critical] => 32
            [can_update_important] => 16
            [can_update_basic] => 8
            [can_change_owner] => 2
            [can_use] => 1
        )    [account_phones] =>排列
        (
            [can_add] => 2
            [can_edit] => 8
            [can_change_status] => 4
            [can_view] => 1
            [can_change_primary] => 16
        )    [联系方式] =>排列
        (
            [can_change_status] => 16
            [can_change_global] => 8
            [can_add] => 32
            [can_view] => 2
            [can_change_primary] => 1
            [can_edit] => 4
        )    [许可权] =>排列
        (
            [can_use] => 1
            [can_update_owner] => 2
            [can_update_dnc_status] => 4
            [can_update_internally_blocked_status] => 8
        )    [manage_phone_calls] =>排列
        (
            [can_change_owner] => 4
            [can_purge] => 8
            [can_change_due_datetime] => 16
            [can_use] => 1
            [can_approve] => 2
            [can_change_urgency_level] => 32
        )    [通知] =>排列
        (
            [can_use] => 1
        )    [other_contacts] =>排列
        (
            [can_save_contact] => 128
            [can_purge_self_created_notes_only] => 32
            [can_purge_all_notes] => 16
            [can_edit_self_created_notes_only] => 8
            [can_create] => 2
            [can_use] => 1
            [can_edit_all_notes] => 4
            [can_save_note] => 64
        )    [phone_calls] =>排列
        (
            [can_use] => 1
            [can_view] => 2
            [can_own_phone] => 4096
            [can_view_client_history] ​​=> 2048
            [can_view_history] ​​=> 1024
            [can_unpurge] => 512
            [can_edit_completed] => 256
            [can_complete_owned_only] => 64
            [can_edit_open] => 128
            [can_complete_any] => 32
            [can_purge] => 16
            [can_insert] => 8
            [can_update] => 4
            [can_create_call_for_others] => 8192
            [can_create_new_call] => 16384
        )    [phone_call_views] =>排列
        (
            [can_use] => 1
            [can_create] => 2
            [can_list_active] => 4
            [can_list_inactive] => 8
            [can_edit] => 16
            [can_disable] => 32
            [can_enable] => 64
        )    [system_security] =>排列
        (
            [can_change_client] => 1
        )    [voice_of_client] =>排列
        (
            [can_edit] => 4
            [can_submit] => 2
            [can_use] => 1
            [can_purge] => 8
            [can_view_completed] => 16
        ))

根据反馈下面我试图使用Dictionary类

这是我目前的code

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Text.RegularEx pressions;
使用MySql.Data.MySqlClient;命名空间POS
{
    类角色
    {
        静态只读正则表达式的二进制=新的正则表达式(^ [01] {1,32} $,RegexOptions.Compiled);
        私人字典<字符串,字符串[]> permissionList =新词典<字符串,字符串[]>();
        公共角色()
        {
            this._definePermissions();
        }        私人诠释_BinToInt(字符串str)
        {
            INT VAL = -1;            如果(binary.IsMatch(STR))
            {
                VAL = Convert.ToInt32(STR,2);
            }            返回VAL;
        }        私人字符串_IntToBin(INT数)
        {
            返回Convert.ToString(数,2);
        }        私人无效_definePermissions()
        {
            变种DB =新dbConnetion();            字符串SQL =SELECT SQL_CACHE sectionName,permissionKey,permissionValue
                         +FROM role_permissions
                         +WHERE状态='启用'
                         +ORDER BY sectionName            在db.getData(SQL,的foreach(VAR我空,R = GT;新SystemPermissions()
                                                           {
                                                                 sectionName = R [sectionName]。的ToString()
                                                               ,permissionKey = R [permissionKey]。的ToString()
                                                               ,permissionValue = R [permissionValue]。的ToString()
                                                           }
                                           )
                    )
            {
                permissionList [sectionName] =新[] {} i.sectionName;
                permissionList [permissionValue] =新[] {this._BinToInt(i.permissionValue)的ToString()};            }
        }    }    类SystemPermissions {
        公共字符串sectionName;
        公共字符串permissionKey;
        公共字符串permissionValue;
    }}

如何能同样的事情可以使用C#做?


解决方案

  

如何我通过定义键和每个条目的价值创造在C#中的数组?


在.NET这将是 字典< TKEY的,TValue> 类。例如:

  VAR词典=新词典<字符串,字符串>();
词典[富] =酒吧;
词典[巴兹] =bazinga;

I need to create an array where I can read data from a database and then create an array with 2 columns.

To get this done in PHP, here is what I have done

private function _getSystemRules()
{
    $rules = $this->db->getDataSet('SELECT SQL_CACHE section_name, privilege_key, privilege_value
                                    FROM user_privileges
                                    WHERE status = 1
                                    ORDER BY section_name');

    foreach($rules as $rule) {
        $this->rolesList[$rule['section_name']][$rule['privilege_key']] = $rule['privilege_value'];
    }
}

UPDATED

The final array will look like this

Array
(
    [accounts] => Array
        (
            [can_own_account] => 4
            [can_view_summary] => 64
            [can_update_critical] => 32
            [can_update_important] => 16
            [can_update_basic] => 8
            [can_change_owner] => 2
            [can_use] => 1
        )

    [account_phones] => Array
        (
            [can_add] => 2
            [can_edit] => 8
            [can_change_status] => 4
            [can_view] => 1
            [can_change_primary] => 16
        )

    [contacts] => Array
        (
            [can_change_status] => 16
            [can_change_global] => 8
            [can_add] => 32
            [can_view] => 2
            [can_change_primary] => 1
            [can_edit] => 4
        )

    [manage_accounts] => Array
        (
            [can_use] => 1
            [can_update_owner] => 2
            [can_update_dnc_status] => 4
            [can_update_internally_blocked_status] => 8
        )

    [manage_phone_calls] => Array
        (
            [can_change_owner] => 4
            [can_purge] => 8
            [can_change_due_datetime] => 16
            [can_use] => 1
            [can_approve] => 2
            [can_change_urgency_level] => 32
        )

    [notification] => Array
        (
            [can_use] => 1
        )

    [other_contacts] => Array
        (
            [can_save_contact] => 128
            [can_purge_self_created_notes_only] => 32
            [can_purge_all_notes] => 16
            [can_edit_self_created_notes_only] => 8
            [can_create] => 2
            [can_use] => 1
            [can_edit_all_notes] => 4
            [can_save_note] => 64
        )

    [phone_calls] => Array
        (
            [can_use] => 1
            [can_view] => 2
            [can_own_phone] => 4096
            [can_view_client_history] => 2048
            [can_view_history] => 1024
            [can_unpurge] => 512
            [can_edit_completed] => 256
            [can_complete_owned_only] => 64
            [can_edit_open] => 128
            [can_complete_any] => 32
            [can_purge] => 16
            [can_insert] => 8
            [can_update] => 4
            [can_create_call_for_others] => 8192
            [can_create_new_call] => 16384
        )

    [phone_call_views] => Array
        (
            [can_use] => 1
            [can_create] => 2
            [can_list_active] => 4
            [can_list_inactive] => 8
            [can_edit] => 16
            [can_disable] => 32
            [can_enable] => 64
        )

    [system_security] => Array
        (
            [can_change_client] => 1
        )

    [voice_of_client] => Array
        (
            [can_edit] => 4
            [can_submit] => 2
            [can_use] => 1
            [can_purge] => 8
            [can_view_completed] => 16
        )

)

Based on the feedback below I am trying to use the Dictionary Class

This is my current code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;

namespace POS
{
    class Roles
    {
        static readonly Regex binary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
        private Dictionary<string, string[]> permissionList = new Dictionary<string, string[]>();


        public Roles()
        {
            this._definePermissions();
        }

        private int _BinToInt(string str)
        {
            int val = -1;

            if (binary.IsMatch(str))
            {
                val = Convert.ToInt32(str, 2);
            }

            return val;
        }

        private string _IntToBin(int number)
        {
            return Convert.ToString(number, 2);
        }

        private void _definePermissions()
        {
            var db = new dbConnetion();

            string sql =   " SELECT SQL_CACHE sectionName, permissionKey, permissionValue "
                         + " FROM role_permissions "
                         + " WHERE status = 'enabled' "
                         + " ORDER BY sectionName ";

            foreach (var i in db.getData(sql, null, r => new SystemPermissions()
                                                           {
                                                                 sectionName = r["sectionName"].ToString()
                                                               , permissionKey = r["permissionKey"].ToString()
                                                               , permissionValue = r["permissionValue"].ToString()
                                                           }
                                           )
                    )
            {
                permissionList["sectionName"] = new[] {i.sectionName};
                permissionList["permissionValue"] = new[] {this._BinToInt(i.permissionValue).ToString()};

            }
        }

    }

    class SystemPermissions{
        public string sectionName;
        public string permissionKey;
        public string permissionValue;
    }

}

How can the same thing be done using c#?

解决方案

How to I create an array in C# by defining the key and the value of each entry?

In .NET that would be the Dictionary<TKey, TValue> class. For example:

var dictionary = new Dictionary<string, string>();
dictionary["foo"] = "bar";
dictionary["baz"] = "bazinga";

这篇关于如何创建通过定义键和每个条目的值在C#中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆