字典有多个键和多个值的每个关键多个、字典、关键

2023-09-03 20:25:10 作者:少了他°我依旧活着

大家好我有一个要求,我必须分配多个密钥和多个键我必须分配多个值

Hi all I am having a requirement where I have to assign multiple keys and for that multiple keys I have to assign multiple values

我的要求如下。我有的EmpID PayYr PayID 为每个员工

My requirement is as follows. I am having EmpID, PayYr and PayID for each employee.

假设我得到我的数据如下:

Assume I get my data as follows:

EmpID  1000    1000  1000   1000
PayYr  2011    2011  2011   2012
PayID    1      2     3      1

我想有我的字典里这样与键值结果字典如下:

I would like to have my dictionary so that the dictionary with key value result is as follows:

1000 - 2011 - 1,2,3
1000 - 2012 - 1

我尝试了一些事情如下:

I tried some thing as follows

public struct Tuple<T1, T2>
{
    public readonly T1 Item1;
    public readonly T2 Item2;

    public Tuple(T1 item1, T2 item2)
    {
        Item1 = item1;
        Item2 = item2;
    }
}

样品code

Sample code

for (int empcnt = 0; empcnt < iEmpID.Length; empcnt++)
    {
        for (int yrcnt = 0; yrcnt < ipayYear.Length; yrcnt++)
        {

            List<int> lst1 = new List<int>();
            var key1 = new Tuple<int, int>(iEmpID[empcnt], ipayYear[yrcnt]);
            if (!dictAddValues.ContainsKey(key1))
            {
                dictAddValues.Add(key1, lst1);
                lst1.Add(lst[yrcnt]);
            }
        }

    }

但我没有得到我的结果,因为我需要让任何人可以帮助我。

But I am not getting my result as i required so can any one help me.

推荐答案

就个人而言,我可能会使用一个字典词典,例如的IDictionary&LT; INT,IDictionary的&LT; INT,IList的&LT; INT&GT;&GT;&GT; 。不是我不完全相信你打算如何访问或促进这种数据;是的将会的有效率有多高我的建议是有很大的影响。从正面看,它将使你 - 相对容易 - 存取数据,当且仅当的你访问它的设置您的字典顺序 (关于第二个想法,只是类型声明本身是如此的丑陋,毫无意义的,你可能想跳过我上面说的。)

Personally, I'd probably use a Dictionary of Dictionaries, e.g. IDictionary<int, IDictionary<int, IList<int>>>. Not I am not entirely sure how you intend to access or facilitate this data; that will have a large impact on how efficient my suggestion is. On the upside, it would allow you to -- relatively easily -- access data, if and only if you access it in the order you set up your dictionaries. (On second thought, simply the type declaration itself is so ugly and meaningless, you might want to skip what I said above.)

如果您要访问字段,而随机,也许一个简单的非规范化的的ICollection&LT;元组LT; INT,INT,INT&GT;&GT; (或同等学历)将不得不这样做的伎俩,与聚集在你的应用程序的其它部分需​​要。 LINQ可以在这里有很大的帮助,尤其是它的聚集,分组和查找功能。

If you are accessing fields rather randomly, maybe a simple denormalized ICollection<Tuple<int, int, int>> (or equivalent) will have to do the trick, with aggregation in other parts of your application as needed. LINQ can help here a lot, especially its aggregation, grouping, and lookup features.

更新:希望这个澄清的:

var outerDictionary = new Dictionary<int, Dictionary<int, List<int>>>();

/* fill initial values
 * assuming that you get your data row by row from an ADO.NET data source, EF, or something similar. */
foreach (var row in rows) {
    var employeeId = (int) row["EmpID"];
    var payYear = (int) row["PayYr"];
    var payId = (int) row["PayID"];


    Dictionary<int, int> innerDictionary;
    if (!outerDictionary.TryGet(employeeId, out innerDictionary)) {
        innerDictionary = new Dictionary<int, int>();
        outerDictionary.Add(employeeId, innerDictionary);
    }

    List<int> list;
    if (!innerDictionary.TryGet(payYear)) {
        list = new List<int>();
        innerDictionary.Add(payYear, list);
    }

    list.Add(payId);
}

/* now use it, e.g.: */
var data = outerDictionary[1000][2011]; // returns a list with { 1, 2, 3 }

把它与一粒盐,虽然;看评论。

Take it with a grain of salt though; see comment.

 
精彩推荐
图片推荐