亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將包含多個數據項的字符串放入對象中

將包含多個數據項的字符串放入對象中

C#
叮當貓咪 2023-04-29 15:42:38
想象一下,我有一些從 SQL 查詢返回的這樣的數據:ContactId                                           Transcript60b0e926-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?tre80e926-2f3a-458d-91f7-fe4a54f1c0f1                [ConfirmAppt-1] : This is another thing然后我將這些數據放入一個List<Tuple<string, string>>()所以每個條目看起來像:Item1:   60b0e926-2f3a-458d-91f7-fe4a21f1c0f1Item2: [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-e4875e1c8ff4Intent: ConfirmAptCount: 2Transcript:[0] Confirm my appointment[1]ok your appointment has been confirmedIntent: RescheudleAptCount: 4Transcript:[0]Reschuedle Appointment[1]Ok, what date?t[2]k, what date?t[3]Ok, what date?t依此類推我列表中的其他項目。我試圖開始這樣做,但是當我開始嘗試將意圖與抄本聯系起來時卻碰壁了。有人可以向我解釋如何做到這一點嗎?
查看完整描述

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


查看完整回答
反對 回復 2023-04-29
  • 1 回答
  • 0 關注
  • 108 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號