1 回答

TA贡献1827条经验 获得超9个赞
弗兰克,使用我的简化评论,按如下方式构建您的类:
public class Contact
{
public string ID { get; }
public IList<Intent> Intents { get; }
public Contact(string id) { ID = id; Intents = new List<Intent>(); }
}
public class Intent
{
public IList<string> Transcripts { get; }
public string Name { get; }
public Intent(string name) { Name = name; Transcripts = new List<string>(); }
}
然后填充所有内容,假设我们已经通过您的查询获取了一个数据表,SELECT 应该按 ContactID 排序,意图:
//Create List<T> of type Contact
IList<Contact> contactInfo = new List<Contact>();
//Temp var for storing the contact to add to list
Contact contact = null;
//Temp var for storing current Intent object
Intent intent = null;
foreach(DataRow row in yourDataTable.Rows)
{
//If we are at a new contact, create the new contact object
if(contact == null || contact.ID != row["ContactId"].ToString())
{
if(contact != null)
contactInfo.Add(contact);
if(contactInfo.Contacts.Where(x => x.ID == row["ContactId"].ToString()).Count() > 0)
{
//set contact to existing contact
contact = contactInfo.Contacts.Where(x => x.ID == row["ContactId"].ToString()).FirstOrDefault();
}
else
{
//set contact to a new Contact Object with the given contact id
contact = new Contact(row["ContactId"].ToString());
}
}
//IF we are at a new Intent create it and add to List of Intents of current contact
if(intent == null || intent.Name != row["Intent"].ToString()
{
//Per your comment Frank
//Check to see if we have an Intent in our contact.Intents list with the current Intent name
if(contact.Intents.Where(x => x.Name == row["Intent"].ToString()).Count() > 0)
{
intent = contact.Intents.Where(x => x.Name == row["Intent"].ToString()).FirstOrDefault();
}
else
{
intent = new Intent(row["Intent"].ToString());
contact.Intents.Add(intent);
}
}
contact.Intents[contact.Intents.Count - 1].Transcripts.Add(row["Transcript"].ToString());
}
contactInfo.Add(contact); //Add the final contact to the list
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报