0

我想知道如何将具有相应值的表的主键存储到字典中。

我的意思是,数据库中有一个名为 ContactPhone 的表(PK = ID、ContactID、PhoneTypeID)

我想要类似的东西:

Dictionary<string, object> 

字符串我的意思是表的主键,最终值将是一个反映表示表的对象。当我的表有一个 PK 时,字典有一个字符串,最终是对象。但如果它是一个组合表,我需要保存许多 PK 来识别我的对象。这样做的最佳方法是什么。

4

1 回答 1

0

一种方法是声明一个ContactPhone类型,然后使用它作为值添加到字典

public class ContactPhone
{
public int ID {get; set);
public int ContactID {get; set;};
public int PhoneTypeID {get; set;};
}

// key is ID, value is the ContactPhone object
Dictionary<int, ContactPhone> contacts = new Dictionary<int, ContactPhone>();

// create a new ContactPhone object
ContactPhone contact = new ContactPhone(1, 1, 1); 
// put the ContactPhone object into the dictionary with ID as the key
contacts.Add(1,contact);  

当然,您必须获取数据,然后循环遍历它以将数据放入字典中。

于 2013-11-01T04:26:12.160 回答