如何在c#中保存Dictionary [英] How do I save Dictionary in c#

查看:442
本文介绍了如何在c#中保存Dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在研究MVC应用程序,我想保存Dictionary< string,>进入我已经拥有的DB​​(首先使用EF,DB生成)。我没有设法找到解决方案,显然EF不支持字典。

我将使用它来关联我的应用程序中的不同散列和神秘算法。下面是示例代码,说明了我想要在db中保存的内容。



Hello. I'm working on MVC application and I want to save Dictionary<string,> into DB I already have (generated with EF, DB first). I've didn't manage to find solutions, apparently EF doesn't support dictionary.
I will use this to associate different hashing and cryptic algorithms in my app. Here is sample code that illustrates what i'd like to save in db.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1 {
    class Program {

        public delegate String AlgDelegte(String a, String b);

        public static String AlgFunc(String a, String b) {
            return String.Concat(a, b);
        }

        public static String Alg1Func(String a, String b) {
            return String.Concat(a, "\nBLA\n", b);
        }
        static void Main(string[] args) {
            AlgDelegte alg = new AlgDelegte(AlgFunc);
            AlgDelegte alg1 = new AlgDelegte(Alg1Func);
            Dictionary<String, AlgDelegte> algorithms = new Dictionary<string, AlgDelegte>();

            algorithms.Add("zeroth", alg);
            algorithms.Add("first", alg1);

            Console.WriteLine(algorithms["first"]("a", "b"));

        }
    }
}





提前致谢。



Thanks in advance.

推荐答案

字典的概念,如果非常接近关系模型。您只需要有一个键值对表,其中字典键可以是主表键。如果必须在单个表中存储多个不同的词典,则可能违反键的唯一性。在这种情况下,使用复合键:一列标识字典实例,另一列标识字典键。请参阅:

http://en.wikipedia.org/wiki/Compound_key [< a href =http://en.wikipedia.org/wiki/Compound_keytarget =_ blanktitle =New Window> ^ ]。



-SA
The concept of dictionary if very close to relational model. You just need to have a table of key-value pairs, where a dictionary key could be a primary table key. If you have to store several different dictionaries in a single table, the key uniqueness can be violated. In this case, use a compound key: one columns identifying a dictionary instance, another one — a dictionary key. Please see:
http://en.wikipedia.org/wiki/Compound_key[^].

—SA


谢谢,这有帮助。但是可以将这些函数委托保存为表中的值吗?
Thanks, this helps. But is it possible to save these function delegates as value in table?


这篇关于如何在c#中保存Dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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