为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

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 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信