开发者

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.

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.

I thought of writing l开发者_Go百科ike 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.

Even I will get Mutilple EmpIDs But if exists already in the dictionary as Key element I don't want to add it again.

(using 2.0 framework)

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

Now if a user select some multiple check boxes and tries to delete i would like to store the values to a Dictionary. I will get multiple EmpID from the selection in which the same EmpID may exists for multiple times. 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.

Sample structure of my 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


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>();


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

    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


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. 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.

As for economy in using EmployeeID once. When using Dictionary<int, MyValue> you have KeyValuePair<int, MyValue> for granted, since the Dictionary is actually a collection of KeyValuePair structures. So you can try to use it if you don't want to create new types or store another int value in your class.

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. But you will have more convenient usage of MyValues instead of passing everywhere KeyValuePairs or MyValue + int.


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜