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

C# 将包含多个数据项的字符串放入对象,c#,sql,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 opt

假设我有一些从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
然后,我将这些数据放入
列表()
中,使每个条目看起来像:

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
……等等

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

选项、确认应用程序、重新安排应用程序

它们各自的转录本,位于两个标签之间。例如,列表中的第二项是:

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
我想看到的是: 我的清单中的其他项目也是如此

我试图开始这样做,但当我开始尝试将意图与成绩单联系起来时,遇到了麻烦

以下是我目前掌握的情况:

        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);
var intentList=newlist();
foreach(意图中的var用户)
{
var intentItem=新的IntentPerUserModel();
intentItem.ContactId=user.Item1;
变量模式=@“\[(.*?\]”;
var matches=Regex.matches(user.Item2,模式);
var intentNames=新列表();
var intentCount=新列表();
foreach(匹配中的匹配)
{
intentNames.Add(match.Groups[1].Value.Split('-')[0]);
intentCount.Add(match.Groups[1].Value.Split('-')[1]);
}
var listOfTranscriptItems=新列表();
intentItem.Intents.Add(新元组(intentNames、intentCount、listOfTranscriptItems));
intentList.Add(intentItem);

有人能给我解释一下如何做到这一点吗?

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

如果您的问题是返回与您在c#code中定义的对象相同的结构,请您定义一个按输出参数返回用户定义类型的存储过程。

Frank,要使用我的简化注释,请按如下方式构造类:

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>(); }
}
公共类联系人
{
公共字符串ID{get;}
公共IList意图{get;}
公共联系人(字符串id){id=id;意图=new List();}
}
公共阶级意图
{
公共IList成绩单{get;}
公共字符串名称{get;}
公共意图(字符串名称){name=name;Transcripts=newlist();}
}
然后,为了填充所有内容,假设我们已经通过您的查询获取了一个数据表。选择应按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
//创建联系人类型列表
IList contactInfo=新列表();
//用于存储要添加到列表中的联系人的临时变量
联系人=空;
//用于存储当前意图对象的临时变量
意向=无效;
foreach(DataTable.Rows中的DataRow行)
{
//如果我们在新联系人处,请创建新联系人对象
if(contact==null | | contact.ID!=行[“ContactId”].ToString())
{
如果(联系人!=null)
contactInfo.Add(联系人);
if(contactInfo.Contacts.Where(x=>x.ID==row[“ContactId”].ToString()).Count()>0)
{
//将联系人设置为现有联系人
contact=contactInfo.Contacts.Where(x=>x.ID==row[“ContactId”].ToString()).FirstOrDefault();
}
其他的
{
//将联系人设置为具有给定联系人id的新联系人对象
contact=新联系人(第[“ContactId”].ToString()行);
}
}
//如果我们有新的意向,创建它并添加到当前联系人的意向列表中
if(intent==null | | intent.Name!=行[“intent”].ToString()
{
//根据你的评论,弗兰克
//检查联系人中是否有意向。意向列表中包含当前意向名称
if(contact.Intents.Where(x=>x.Name==row[“Intent”].ToString()).Count()>0)
{
intent=contact.Intents.Where(x=>x.Name==row[“intent”].ToString()).FirstOrDefault();
}
其他的
{
intent=新intent(第[“intent”].ToString()行);
contact.Intents.Add(intent);
}
}
contact.Intents[contact.Intents.Count-1].Transcripts.Add(第[“Transcript”].ToString()行);
}
contactInfo.Add(contact);//将最后一个联系人添加到列表中

Frank,使用我的简化注释,将您的类结构如下:

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>(); }
}
公共类联系人
{
公共字符串ID{get;}
公共IList意图{get;}
公共联系人(字符串id){id=id;意图=new List();}
}
公共阶级意图
{
公共IList成绩单{get;}
公共字符串名称{get;}
公共意图(字符串名称){name=name;Transcripts=newlist();}
}
然后,为了填充所有内容,假设我们已经通过您的查询获取了一个数据表。选择应按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
//创建联系人类型列表
IList contactInfo=新列表();
//用于存储要添加到列表中的联系人的临时变量
联系人=空;
//用于存储当前意图对象的临时变量
意向=无效;
foreach(DataTable.Rows中的DataRow行)
{
//如果我们在新联系人处,请创建新联系人对象
if(contact==null | | contact.ID!=行[“ContactId”].ToString())
{
如果(联系人!=null)
contactInfo.Add(联系人);
if(contactInfo.Contacts.Where(x=>x.ID==row[“ContactId”].ToString()).Count()>0)
{
//将联系人设置为现有联系人
contact=contactInfo.Contacts.Where(x=>x.ID==row[“ContactId”].ToString()).FirstOrDefault();
}
其他的
{
//将联系人设置为具有给定联系人id的新联系人对象
contact=新联系人(第[“ContactId”].ToString()行);
}