将包含多个数据项的字符串放入对象中

想象一下,我有一些从 SQL 查询返回的这样的数据:


ContactId                                           Transcript

60b0e926-2f3a-458d-91f7-fe4a21f1c0f1                [Options-13] : Give me options ,Give me options ,Give me options ,Give me options ,Give me options ,Give me options ,Give me options ,I can do this that and the other ,Awesome! ,Awesome! ,Awesome! ,Awesome! ,Awesome!

d463d996-78cc-428e-8a76-e4875e1c8ff4                [ConfirmApt-2] : Confirm my appointment ,ok your appointment has been confirmed [RescheudleApt-4] : Reschuedle Appointment ,Ok, what date?t ,Ok, what date?t ,Ok, what date?t

re80e926-2f3a-458d-91f7-fe4a54f1c0f1                [ConfirmAppt-1] : This is another thing

然后我将这些数据放入一个List<Tuple<string, string>>()所以每个条目看起来像:


Item1:   60b0e926-2f3a-458d-91f7-fe4a21f1c0f1

Item2: [Options-13] : Give me options ,Give me options ,Give me options ,Give me options ,Give me options ,Give me options ,Give me options ,I can do this that and the other ,Awesome! ,Awesome! ,Awesome! ,Awesome! ,Awesome!



Item1:d463d996-78cc-428e-8a76-e4875e1c8ff4  

Item2: [ConfirmApt-2] : Confirm my appointment ,ok your appointment has been confirmed$ [RescheudleApt-4] : Reschuedle Appointment ,Ok, what date?t ,Ok, what date?t ,Ok, what date?t

..等等


我正在尝试创建一个对象,以正确的格式保存所有这些数据。我要采用的格式本质上是链接我的意图


选项、ConfirmAppt、RescheduleAppt


带有各自的成绩单,位于两个标签之间。例如,列表中的第二项是:


d463d996-78cc-428e-8a76-e4875e1c8ff4                [ConfirmApt-2] : Confirm my appointment ,ok your appointment has been confirmed [RescheudleApt-4] : Reschuedle Appointment ,Ok, what date?t ,Ok, what date?t ,Ok, what date?t

我想看到的是:

Key: d463d996-78cc-428e-8a76-e4875e1c8ff4

Intent: ConfirmApt

Count: 2

Transcript:

[0] Confirm my appointment

[1]ok your appointment has been confirmed


Intent: RescheudleApt

Count: 4

Transcript:

[0]Reschuedle Appointment

[1]Ok, what date?t

[2]k, what date?t

[3]Ok, what date?t

依此类推我列表中的其他项目。


我试图开始这样做,但是当我开始尝试将意图与抄本联系起来时却碰壁了。


有人可以向我解释如何做到这一点吗?


叮当猫咪
浏览 93回答 1
1回答

素胚勾勒不出你

弗兰克,使用我的简化评论,按如下方式构建您的类:public class Contact{&nbsp; &nbsp; public string ID { get; }&nbsp; &nbsp; public IList<Intent> Intents { get; }&nbsp; &nbsp; public Contact(string id) { ID = id; Intents = new List<Intent>(); }}public class Intent{&nbsp; &nbsp; public IList<string> Transcripts { get; }&nbsp; &nbsp; public string Name { get; }&nbsp; &nbsp; public Intent(string name) { Name = name; Transcripts = new List<string>(); }}然后填充所有内容,假设我们已经通过您的查询获取了一个数据表,SELECT 应该按 ContactID 排序,意图://Create List<T> of type ContactIList<Contact> contactInfo = new List<Contact>();//Temp var for storing the contact to add to listContact contact = null;//Temp var for storing current Intent objectIntent intent = null;foreach(DataRow row in yourDataTable.Rows){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//If we are at a new contact, create the new contact object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(contact == null || contact.ID != row["ContactId"].ToString())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(contact != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contactInfo.Add(contact);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(contactInfo.Contacts.Where(x => x.ID == row["ContactId"].ToString()).Count() > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set contact to existing contact&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contact = contactInfo.Contacts.Where(x => x.ID == row["ContactId"].ToString()).FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//set contact to a new Contact Object with the given contact id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contact = new Contact(row["ContactId"].ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//IF we are at a new Intent create it and add to List of Intents of current contact&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(intent == null || intent.Name != row["Intent"].ToString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Per your comment Frank&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Check to see if we have an Intent in our contact.Intents list with the current Intent name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(contact.Intents.Where(x => x.Name == row["Intent"].ToString()).Count() > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;intent = contact.Intents.Where(x => x.Name == row["Intent"].ToString()).FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;intent = new Intent(row["Intent"].ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contact.Intents.Add(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contact.Intents[contact.Intents.Count - 1].Transcripts.Add(row["Transcript"].ToString());&nbsp; &nbsp;}contactInfo.Add(contact); //Add the final contact to the list
打开App,查看更多内容
随时随地看视频慕课网APP