自定义了一个数据对象:
pulic class Reply
{
  private string replyID;
  public string ReplyID
  {
     get {reutrn this.replyID;}
     set {set this.replyID=value;}
  }
  private string additionID;
  public string AdditionID
  {
     get {reutrn this.additionID;}
     set {set this.additionID=value;}
  }
  private List<string> additionIDList
  public List<string> AdditionIDList
  {
     get {reutrn this.additionIDList;}
     set {set this.additionIDList=value;}
  }
}
ReplyID是主键,和AdditionID是一对多的数据关系。然后自定义了一个List泛型链表集合,List<Reply>,它的对象类型是上面定义的数据对象,
然后将数据库检索出来的数据集合放到List<Reply>集合中。
假如List<Reply>目前保存的集合如下所示:
    ReplyID                     AdditionID                
20100429093415443022 20100429093405132019   
20100429093415443022 20100429093405132020
20100429093415443022 20100429093405132023
20100429093731688035 20100429093722375031
20100429093731688035 20100429093722375035   
20100429093731688035 20100429093727109033   
大家应该看得出来:一个ReplyID对应多个AdditonID,现在我想对这个结果集做下处理
效果变成这样:
   ReplyID                                           additionIDList                           20100429093415443022 20100429093405132019,20100429093405132020,20100429093405132023
20100429093731688035 20100429093722375031,20100429093722375035,20100429093727109033 说白了,就是根据一个ReplyID对应多个AdditonID,把每一个ReplyID对应的AdditionID组合成一个字符串。
请问具体用C#编码如何实现?谢谢大家不惜赐教。呵呵
                     

解决方案 »

  1.   

    效果编码乱了,修正下:
            ReplyID                                     additionIDList 
    20100429093415443022  20100429093405132019,20100429093405132020,20100429093405132023
    20100429093731688035  20100429093722375031,20100429093722375035,20100429093727109033 
      

  2.   

          ReplyID                           additionIDList 
    20100429093415443022  20100429093405132019,20100429093405132020,20100429093405132023
    20100429093731688035  20100429093722375031,20100429093722375035,20100429093727109033 
      

  3.   

    从数据库从检索出来:
    select additionIDList  from Reply where ReplyId='20100429093415443022 '
    这样就得到:
    20100429093405132019,20100429093405132020,20100429093405132023
    用一个循环读出返回的结果,拼装就好
      

  4.   

    List<Reply> 执行排序Sort方法
      

  5.   

    类中重载ToString方法:
            public override string ToString()
            {
                return replyID;
            }
      

  6.   

    您理解错了,additionIDList是个自定义的变量,公开属性就是对additionID进行封装的
      

  7.   

    刚写了一个 泛型排序的 模型程序,可参考一下:http://blog.csdn.net/sxldfang/archive/2010/05/05/5559430.aspx
      

  8.   


    List<string> list= GetList();//get data
    List<string> resultList=list.Find(PredicateItems);//PredicateItems是个委托        private bool PredicateItems(item targetItem)
            {
                if (targetItem.itemID == _itemID)//_itemID自定
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
      

  9.   

    List<reply> replylst;//指已取到值的集合
    foreach(string info in replylst){
       for(int i=0;i<info.Count;i++){
           string str+= info[i].AdditionID+' ';
            }
        string[] str=str.EndTrim(' ').Split(' ');
    }
      

  10.   

    string str="";
    foreach( reply r in replylst){
      str+= i.AdditionID+",";
      }
    declare   @sql   varchar(100)   
          set   @sql=''   
          select   @sql=@sql+','+B   from   tb   where  a=@id     
          return(stuff(@sql,1,1,''))   
      

  11.   

    兄弟做了测试,可以实现,谢谢大家不惜赐教,呵呵。
    我把测试代码贴出来,大家交流下,很简单的。
    数据对象:
    public class test
        {
            private string replyID;        public string ReplyID
            {
                get { return replyID; }
                set { replyID = value; }
            }        private string attachment;        public string Attachment
            {
                get { return attachment; }
                set { attachment = value; }
            }        private List<string> attachmentList = new List<string>();        public List<string> AttachmentList
            {
                get { return attachmentList; }
                set { AttachmentList = value; }
            }
        }
    窗体实现(wpf):
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Model;namespace WPF
    {
        /// <summary>
        /// Window1.xaml 的交互逻辑
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                test md1 = new test();
                md1.ReplyID = "1";
                md1.Attachment = "01";
                test md2 = new test();
                md2.ReplyID = "1";
                md2.Attachment = "02";
                test md3 = new test();
                md3.ReplyID = "1";
                md3.Attachment = "03";
                test md4 = new test();
                md4.ReplyID = "2";
                md4.Attachment = "04";
                test md5 = new test();
                md5.ReplyID = "2";
                md5.Attachment = "05";
                test md6 = new test();
                md6.ReplyID = "3";
                md6.Attachment = "06";
                test md7 = new test();
                md7.ReplyID = "4";
                list.Add(md1);
                list.Add(md2); 
                list.Add(md3);
                list.Add(md4);
                list.Add(md5);
                list.Add(md6);
                list.Add(md7);
            }        List<test> list = new List<test>();
                          
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                // 实现对List的重新封装。
                for (int i = 0; i < list.Count; i++)
                {
                    test modelPrev = list[i];
                    if (modelPrev.Attachment != null)
                    {
                        modelPrev.AttachmentList.Add(modelPrev.Attachment);
                    }
                    for (int j = i + 1; j < list.Count - 1; j++)
                    {                    
                        test modelAfter = list[j];
                        if (modelAfter.ReplyID == modelPrev.ReplyID)
                        {
                            list[i].AttachmentList.Add(modelAfter.Attachment);
                            modelPrev.Attachment += ","+modelAfter.Attachment;
                            list.RemoveAt(j);
                            j--;
                        }
                    }
                }
                this.listview.ItemsSource = list;
            }
        }
    }
    再次谢谢大家。