如何根据我的要求使用Dictionary?

时间:2011-09-09 08:08:57

标签: c# dictionary c#-2.0

大家好我有选择多个值然后删除的要求。我会列出一些EMployee IDs以及DatesPayID值的列表。

我将获得所有这些字段。现在我想将此添加到字典元素,例如特定EmpID,我必须将Date and PayID指定为键。

我想过像这样写作

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

但我在处理这个问题上有点困惑,所以任何人都可以按照我的要求给我一个实现的想法。

这是我的要求,如果EmpID1,我会Multiple PayIDs and Dates 1 and System Date

即使我会得到Mutilple EmpIDs但如果字典中已存在Key元素,我不想再添加它。

(使用2.0框架)

我该怎么做?

这是我正在做的事情,我将有一个带有复选框的网格来删除记录。此gridview包含EmpID PayIDDate

现在,如果用户选择了多个复选框并尝试删除,我想将值存储到Dictionary。我将从选择中获得多个EmpID,其中多次可能存在相同的EmpID。所以我需要做的是我希望将相应的值存储到DictionaryEmpIDKey值,Values将字典存储为多个,这将是PayID and Date

我的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我想将1,2 and the 2 dates指定为值

5 个答案:

答案 0 :(得分:0)

为什么不直接拥有包含所有员工信息的员工类型,例如

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

然后就像这样的字典:

Dictionary<int, Employee>();

答案 1 :(得分:0)

我在暗示什么,

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

您可以迭代字典或搜索员工

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

答案 2 :(得分:0)

如果您想要找到要删除的行,那么最好创建代表整行的MyValue。请记住,词典不允许多次添加一个键。此外,如果要查找行,则不需要将其值存储在不同的列表中,因为您很容易丢失哪个列对应哪行的值。因此,最好以这种方式存储要删除的行:

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

现在您可以使用EmployeeID作为键来查找要删除的行列表。

一次使用EmployeeID的经济性。使用Dictionary<int, MyValue>时,KeyValuePair<int, MyValue>被认为是理所当然的,因为Dictionary实际上是KeyValuePair结构的集合。因此,如果您不想在类中创建新类型或存储另一个int值,则可以尝试使用它。

但是我认为在MyValue中添加int并不是什么大问题 - 对主集合进行反向引用总是有用的,如果与指针类型相比,int相当小。但是你可以更方便地使用MyValues而不是传递到任何地方KeyValuePairs或MyValue + int。

答案 3 :(得分:0)

您可以在词典中使用词典。

例如:

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

有关
Dictionary<EmpID, Dictionary<PayID, Date>>

因此,要获得员工特定付款ID的日期

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

    int EmpID = 1;
    int PayRateID = 2;

    DateTime date = dict[EmpID][PayRateID];

显然,您需要先检查字典中是否存在值

答案 4 :(得分:0)

任何人都能说出这个

的错误
   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();

我的词典如下

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