怎么对List<自定义数据对象>进行如下的操作?

frustrate2 2010-05-05 09:38:23
自定义了一个数据对象:
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#编码如何实现?谢谢大家不惜赐教。呵呵





...全文
228 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
draglonglong 2010-08-06
  • 打赏
  • 举报
回复
学习。。。
frustrate2 2010-05-18
  • 打赏
  • 举报
回复
地方地方
frustrate2 2010-05-06
  • 打赏
  • 举报
回复
兄弟做了测试,可以实现,谢谢大家不惜赐教,呵呵。
我把测试代码贴出来,大家交流下,很简单的。
数据对象:
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;
}
}
}
再次谢谢大家。
frustrate2 2010-05-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 computerfox 的回复:]
从数据库从检索出来:
select additionIDList from Reply where ReplyId='20100429093415443022 '
这样就得到:
20100429093405132019,20100429093405132020,20100429093405132023
用一个循环读出返回的结果,拼装就好
[/Quote]
您理解错了,additionIDList是个自定义的变量,公开属性就是对additionID进行封装的
sxldfang 2010-05-05
  • 打赏
  • 举报
回复
类中重载ToString方法:
public override string ToString()
{
return replyID;
}
sxldfang 2010-05-05
  • 打赏
  • 举报
回复
List<Reply> 执行排序Sort方法
捷哥1999 2010-05-05
  • 打赏
  • 举报
回复
从数据库从检索出来:
select additionIDList from Reply where ReplyId='20100429093415443022 '
这样就得到:
20100429093405132019,20100429093405132020,20100429093405132023
用一个循环读出返回的结果,拼装就好
frustrate2 2010-05-05
  • 打赏
  • 举报
回复
ReplyID additionIDList
20100429093415443022 20100429093405132019,20100429093405132020,20100429093405132023
20100429093731688035 20100429093722375031,20100429093722375035,20100429093727109033
frustrate2 2010-05-05
  • 打赏
  • 举报
回复
效果编码乱了,修正下:
ReplyID additionIDList
20100429093415443022 20100429093405132019,20100429093405132020,20100429093405132023
20100429093731688035 20100429093722375031,20100429093722375035,20100429093727109033
mngzilin 2010-05-05
  • 打赏
  • 举报
回复
遍历下
wuyq11 2010-05-05
  • 打赏
  • 举报
回复
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,''))
yongchaochu 2010-05-05
  • 打赏
  • 举报
回复
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(' ');
}
GT7466 2010-05-05
  • 打赏
  • 举报
回复

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;
}
}
GT7466 2010-05-05
  • 打赏
  • 举报
回复
使用List<T>的Predicate
sxldfang 2010-05-05
  • 打赏
  • 举报
回复
刚写了一个 泛型排序的 模型程序,可参考一下:

http://blog.csdn.net/sxldfang/archive/2010/05/05/5559430.aspx

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧