简体   繁体   English

如何根据需要使用字典?

[英]How can I use Dictionary for my requirement?

Hi all I am having a requirement like selecting multiple values and then to delete. 大家好,我有一个要求,例如选择多个值然后删除。 I will have some list of EMployee IDs along with Dates and PayID values. 我将获得一些EMployee IDs以及DatesPayID值的列表。

I will get all this fields in multiple. 我将把所有这些字段分成多个。 Now i would like to add this to dictionary element like for particular EmpID I have to assign Date and PayID as keys. 现在,我想将此添加到字典元素中,例如对于特定的EmpID我必须将Date and PayID分配为键。

I thought of writing like this 我想到这样写

public struct MyValue
{
    public List<int> lst;
    public List<int> lst1;
    public List<DateTime> lstdt;
}

public class MyDictionary : Dictionary<int, MyValue>
{
    public void Add(int key, List<int> lst1, List<int> lst2, List<DateTime> lstDt1)
    {
        MyValue v1=new MyValue();
        v1.lst = lst1;
        v1.lst1 = lst2;
        v1.lstdt = lstDt1;
        this.Add(key, v1);
    }
}

But I am little bit confused in dealing with this so can any one give me an idea to implement as per my requirement. 但是我对此有点困惑,所以任何人都可以按照我的要求给我一个实现的想法。

This is my requirement if I have EmpID as 1 for this I will have Multiple PayIDs and Dates like 1 and System Date . 这是我的要求,如果我的EmpID1 ,则我将有Multiple PayIDs and Dates例如1 and System Date

Even I will get Mutilple EmpIDs But if exists already in the dictionary as Key element I don't want to add it again. 甚至我都会得到Mutilple EmpIDs但是如果字典中已经存在作为Key元素,那么我不想再次添加它。

(using 2.0 framework) (使用2.0框架)

How can I do this? 我怎样才能做到这一点?

Here is what i am doing i will have a grid with check boxes to delete the records. 这是我在做什么,我将有一个带有复选框的网格以删除记录。 This gridview holds EmpID PayID and Date gridview包含EmpID PayIDDate

Now if a user select some multiple check boxes and tries to delete i would like to store the values to a Dictionary . 现在,如果用户选中了多个复选框并尝试删除,我想将值存储到Dictionary I will get multiple EmpID from the selection in which the same EmpID may exists for multiple times. 我将从选择中获得多个EmpID ,其中同一EmpID可能存在多次。 So what i need to do is i would like to store the corresponding Values to a Dictionary with EmpID as Key value and the Values to the dictionary as multiple which will be the PayID and Date . 所以我需要做的是我想的相应值的存储DictionaryEmpIDKey价值和Values ,以字典为多,这将是PayID and Date

Sample structure of my Gridview 我的Gridview样本结构

       Chkbox     EmpID    PayID     Date
        chk        123       1     8-31-2011
        chk        123       2     10-31-2011
        chk        1234       1     18-31-2011
        chk        1234       1     19-31-2011

for EmpID 123 i would like to assign 1,2 and the 2 dates as Values 对于EmpID 123我想分配1,2 and the 2 dates作为值

Why don't you just have an employee type that contains all your employee information like 您为什么不拥有包含所有员工信息的员工类型,例如

public class Employee
{
   List1<>()
   List2<>()
   List3<>()
}

And then just have a dictionary like so: 然后像这样拥有一个字典:

Dictionary<int, Employee>();

What I'm suggesting, 我的建议是

class Employee
{
   public int EmployeeID {get;set;}
   public DateTime Date {get;set;}
   public int PayID {get;set;}
}

class MyDictionary
{
    private static Dictionary<int, List<Employee>> dict = new Dictionary<int, List<Employee>>();
    public static void Add(Employee obj)
    {
        if (dict.ContainsKey(obj.EmployeeID))
        {
            dict[obj.EmployeeID].Add(obj);
        }
        else
        {
            dict.Add(obj.EmployeeID, new List<Employee>() {obj });
        }
    }
    public static List<Employee> GetEmps(int key)
    {
        if (dict.ContainsKey(key))
            return dict[key];
        return null;
    }
} 

You can iterate the dictionary or search an employee 您可以迭代字典或搜索员工

MyDictionary.Add(new Employee() { EmployeeID = 1, PayID = 1 });
 MyDictionary.Add(new Employee() { EmployeeID = 1, PayID = 2 });
 MyDictionary.Add(new Employee() { EmployeeID = 2, PayID = 3 });
 MyDictionary.Add(new Employee() { EmployeeID = 3, PayID = 1 });

 foreach (Employee e in MyDictionary.GetEmps(1))
    Console.WriteLine(e.EmployeeID + " " + e.PayID);  

If you are want to find the rows to be deleted, then its better to create MyValue that represents the whole row. 如果要查找要删除的行,则最好创建代表整个行的MyValue。 Remember that Dictionary doesn't allow one key to be added several times. 请记住,字典不允许多次添加一个键。 Moreover, if you want to find the rows then you don't need to store their values in different lists, because you can easily lose the track of which value in which list corresponds to which row. 此外,如果要查找行,则无需将它们的值存储在不同的列表中,因为您很容易失去跟踪哪个列表与哪个行相对应的值。 So it's better to store rows to be deleted in such way: 因此,最好以这种方式存储要删除的行:

Dictionary<int, List<MyValue>> rowsToBeDeleted = new Dictionary<int, List<MyValue>> ();
class MyValue
{
  int EmpID;
  int PayID;
  DateTime Date;
}

rowsToBeDeleted.Add(123, new List<MyValue>);
rowsToBeDeleted[123].Add(123, 1, new DateTime(2011,8,31);
rowsToBeDeleted[123].Add(123, 2, new DateTime(2011,10,31);

rowsToBeDeleted.Add(1234, new List<MyValue>);
rowsToBeDeleted[1234].Add(1234, 1, new DateTime(2011,18,31); // month #18 - wow!
rowsToBeDeleted[1234].Add(1234, 2, new DateTime(2011,19,31);

Now you are able by using EmployeeID as a key find a list of rows to be deleted. 现在,您可以使用EmployeeID作为键,找到要删除的行的列表。

As for economy in using EmployeeID once. 至于使用一次EmployeeID的经济性。 When using Dictionary<int, MyValue> you have KeyValuePair<int, MyValue> for granted, since the Dictionary is actually a collection of KeyValuePair structures. 使用Dictionary<int, MyValue>您将KeyValuePair<int, MyValue>理所当然的,因为Dictionary实际上是KeyValuePair结构的集合。 So you can try to use it if you don't want to create new types or store another int value in your class. 因此,如果您不想创建新类型或在类中存储另一个int值,则可以尝试使用它。

However I think that adding int into MyValue is not a big deal - it's always useful to have some backreference to master collection and int is rather small if compared to pointer type. 但是我认为向MyValue中添加int没什么大不了-对主集合进行一些反向引用总是很有用的,并且与指针类型相比int很小。 But you will have more convenient usage of MyValues instead of passing everywhere KeyValuePairs or MyValue + int. 但是您将可以更方便地使用MyValues,而不是将KeyValuePairs或MyValue + int传递到任何地方。

You Could Use a Dictionary within a Dictionary. 您可以在词典中使用词典。

eg: 例如:

Dictionary<int, Dictionary<int, DateTime>> dict = new Dictionary<int, Dictionary<int, DateTime>>();

which would relate to 这将与

Dictionary<EmpID, Dictionary<PayID, Date>>

So to get the day of a particular Pay ID for an employee would be 因此,要获取某位员工特定薪水ID的日期是

    Dictionary<int, Dictionary<int, DateTime>> dict = new Dictionary<int, Dictionary<int, DateTime>>();

    int EmpID = 1;
    int PayRateID = 2;

    DateTime date = dict[EmpID][PayRateID];

Obviously you need to check if the Values exist in the dictionary first 显然,您需要先检查值是否存在于字典中

Can any one tell what's wrong in this 谁能告诉我这是怎么回事

   arrEmpID.Add(1);
    arrEmpID.Add(1);
    arrEmpID.Add(2);
    arrEmpID.Add(2);

    arrPay.Add(1);
    arrPay.Add(1);
    arrPay.Add(2);
    arrPay.Add(2);


    DateTime dt = DateTime.Now;
    DateTime dt1 = DateTime.Now.AddMinutes(1);
    DateTime dt2 = DateTime.Now.AddMinutes(1);
    DateTime dt3 = DateTime.Now.AddMinutes(1);

    arrPayID.Add(dt);
    arrPayID.Add(dt1);
    arrPayID.Add(dt2);
    arrPayID.Add(dt3);

    EmpIDs = (int[])arrEmpID.ToArray(typeof(int));
    Dates = (DateTime[])arrPayID.ToArray(typeof(DateTime));
    PayIDs = (int[])arrPay.ToArray(typeof(int));

for (int i = 0; i < EmpIDs.Length; i++)
    {
        if (!(d.ContainsKey(EmpIDs[i])))
        {
            List<int> values = new List<int>();
            List<DateTime> ldt = new List<DateTime>();
            d.Add(EmpIDs[i], values, ldt);
        }
        d[EmpIDs[i]].lst.Add(PayIDs[i]);
        d[EmpIDs[i]].lstdt.Add(Dates[i]);
    }

   foreach (int EmpID in d.Keys)
    {
        foreach (int pID in d[EmpID].lst) // I just missed out this values
        {
            sb.Append(pID);
            sb.AppendLine(" ");
        }
        foreach (DateTime dtTimes in d[EmpID].lstdt)  // I just missed out this values
        {

            sb1.Append(dtTimes.ToString("MMddyyyy"));
            sb1.AppendLine(" ");
        }
    }

    Label1.Text = sb.ToString();
    Label2.Text = sb1.ToString();

I have my dictionary as follows 我的字典如下

public struct MyValue
{
    public List<int> lst;
    public List<DateTime> lstdt;
}

public class MyDictionary : Dictionary<int, MyValue>
{
    public void Add(int key, List<int> lst1, List<DateTime> lstDt1)
    {
        MyValue v1 = new MyValue();
        v1.lst = lst1;
        v1.lstdt = lstDt1;
        this.Add(key, v1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM