自定义了一个数据对象:
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#编码如何实现?谢谢大家不惜赐教。呵呵
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#编码如何实现?谢谢大家不惜赐教。呵呵
解决方案 »
- 用oledb连接excel,GetSchema得到同一个Sheet两次的问题
- VS2008中代码定义窗口如何显示工厂模式的dal数据库操作层?
- winfrom textbox 下拉列表
- 局域网防攻击问题
- .net C#语言软件开发 如何使窗口自动最大化
- 菜鸟问题!关于ContextMenuStrip的.
- VS2008 安装完了 工具箱里面怎么还没有 水晶报表 ???
- [求助]如何在WinForm裏面發送CTRL+V指令(不用鍵盤).在線等
- 怎么判段数据库image类型的数据为空?
- c# gridview 按某列,导出分组信息!
- formview编辑 更新问题
- winform 所有操作写入日志表
ReplyID additionIDList
20100429093415443022 20100429093405132019,20100429093405132020,20100429093405132023
20100429093731688035 20100429093722375031,20100429093722375035,20100429093727109033
20100429093415443022 20100429093405132019,20100429093405132020,20100429093405132023
20100429093731688035 20100429093722375031,20100429093722375035,20100429093727109033
select additionIDList from Reply where ReplyId='20100429093415443022 '
这样就得到:
20100429093405132019,20100429093405132020,20100429093405132023
用一个循环读出返回的结果,拼装就好
public override string ToString()
{
return replyID;
}
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;
}
}
foreach(string info in replylst){
for(int i=0;i<info.Count;i++){
string str+= info[i].AdditionID+' ';
}
string[] str=str.EndTrim(' ').Split(' ');
}
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,''))
我把测试代码贴出来,大家交流下,很简单的。
数据对象:
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;
}
}
}
再次谢谢大家。