将具有多个数据项的字符串放入对象

时间:2019-06-21 14:43:59

标签: c# sql

想象一下我有一些从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

以此类推,以查看列表中的其他项目。

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

这是我到目前为止所拥有的:

        var intentList = new List<IntentPerUserModel>();
        foreach (var user in intents)
        {
            var intentItem = new IntentPerUserModel();

            intentItem.ContactId = user.Item1;

            var pattern = @"\[(.*?)\]";
            var matches = Regex.Matches(user.Item2, pattern);
            var intentNames = new List<string>();
            var intentCount = new List<string>();

            foreach(Match match in matches)
            {
                intentNames.Add(match.Groups[1].Value.Split('-')[0]);
                intentCount.Add(match.Groups[1].Value.Split('-')[1]);
            }

            var listOfTranscriptItems = new List<string>();



            intentItem.Intents.Add(new Tuple<List<string>, List<string>, List<string>>(intentNames, intentCount, listOfTranscriptItems));
            intentList.Add(intentItem);

有人可以向我解释如何实现吗?

2 个答案:

答案 0 :(得分:1)

坦率地说,为了使用我的简化注释,请按如下所示构造您的类:

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>(); }
}

然后填充所有内容,假设我们已经通过您的查询获取了一个DataTable,那么SELECT应该是按ContactID,Intent进行的:

//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 :(得分:-1)

如果您的问题是返回与您在c#代码中定义的对象相同的结构,然后定义一个存储过程,该存储过程将通过输出参数返回用户定义的类型。