C# 如何使用字典来满足我的需求?
嗨,所有我有一个像选择多个值,然后删除的要求。我将有一些C# 如何使用字典来满足我的需求?,c#,dictionary,c#-2.0,C#,Dictionary,C# 2.0,嗨,所有我有一个像选择多个值,然后删除的要求。我将有一些EMployee id列表,以及Dates和PayID值 我将在多个字段中获取所有这些字段。现在我想把它添加到dictionary元素中,比如对于特定的EmpID,我必须将Date和PayID指定为键 我想过这样写 public struct MyValue { public List<int> lst; public List<int> lst1; public List<DateTi
EMployee id
列表,以及Dates
和PayID
值
我将在多个字段中获取所有这些字段。现在我想把它添加到dictionary元素中,比如对于特定的EmpID
,我必须将Date和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);
}
}
对于
EmpID
123
,我想将1,2和2个日期
指定为值为什么不创建一个包含所有员工信息的员工类型,如
public class Employee
{
List1<>()
List2<>()
List3<>()
}
公共类员工
{
清单1()
清单2()
清单3()
}
然后有一本这样的字典:
Dictionary<int, Employee>();
Dictionary();
我的建议
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;
}
}
如果要查找要删除的行,那么最好创建表示整行的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);
Dictionary rowsToBeDeleted=newdictionary();
类MyValue
{
int EmpID;
int PayID;
日期时间日期;
}
添加(123,新列表);
rowsToBeDeleted[123]。添加(123,1,新日期时间(2011,8,31);
rowsToBeDeleted[123]。添加(123,2,新日期时间(2011,10,31);
添加(1234,新列表);
rowsToBeDeleted[1234]。添加(1234,1,新日期时间(2011,18,31);//月份#18-哇!
rowsToBeDeleted[1234]。添加(1234,2,新日期时间(2011,19,31);
现在,您可以使用EmployeeID作为键来查找要删除的行列表
至于一次性使用EmployeeID的经济性。当使用Dictionary
时,您理所当然地拥有KeyValuePair
,因为Dictionary实际上是KeyValuePair结构的集合。因此,如果您不想在类中创建新类型或存储另一个int值,您可以尝试使用它
然而,我认为将int添加到MyValue中并不是什么大不了的事——对主集合进行一些反向引用总是很有用的,而且int与指针类型相比非常小。但是,使用MyValue将更方便,而不是到处传递KeyValuePairs或MyValue+int。您可以在一个字典中使用字典非官方的 例如:
Dictionary dict=new Dictionary();
这与
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];
Dictionary dict=new Dictionary();
int-EmpID=1;
int PayRateID=2;
日期时间日期=dict[EmpID][PayRateID];
显然,您需要首先检查字典中是否存在这些值有人能说出这其中的错误吗
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();
arrEmpID.Add(1);
增加(1);
增加(2);
增加(2);
增加第(1)款;
增加第(1)款;
增加第(2)款;
增加第(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
我的字典如下
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);
}
}
公共结构MyValue
{
公共列表lst;
公开名单;
}
公共类MyDictionary:Dictionary
{
公共无效添加(int键、列表lst1、列表lstDt1)
{
MyValue v1=新的MyValue();
v1.lst=lst1;
v1.lstdt=lstDt1;
添加(键,v1);
}
}
我真的不明白这个问题。也许可以在示例数据流中添加一些上下文?我不确定是否取消了您的问题。对我来说,听起来您有一个日期和薪资id列表,属于员工id。如果是这样,我将使用两个字典,两个字典都以员工id为键,然后是薪资id或者日期作为值。或者pay id和date是一对值吗?@Marc Gravell
我编辑了我的问题,你可以看看我如何循环foreach
为字典键添加相应的值,这是EmpID
@用户请查看我的帖子。我不确定这对你有什么帮助。
Dictionary<int, Dictionary<int, DateTime>> dict = new Dictionary<int, Dictionary<int, DateTime>>();
int EmpID = 1;
int PayRateID = 2;
DateTime date = dict[EmpID][PayRateID];
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);
}
}