C#读取数据库写文本ID相同行连接
读取的数据库结构如下(数据库中大概有7万条数据):
1 a1
1 a2
1 a3
2 b1
3 c1
3 c2
写入文本的格式如下:
1 a1 a2 a3
2 b1
3 c1 c2
请问牛牛们,我该怎么做
[解决办法]
DataClasses1DataContext db = new DataClasses1DataContext();
foreach(int key in db.Attendances.Select(s => s.LocationId).Distinct()
{
Console.Write(key);
foreach(string text in db.Attendances.Where(s => s.LocationId == key).Select(s => s.Text))
{
Console.Write(text);
}
}
create table tb(id int ,context varchar(10))
insert into tb
select 1,'a1'
union all select 1,'a2'
union all select 1,'a3'
union all select 2,'b1'
union all select 3,'c1'
union all select 3,'c2'
go
create function dbo.fn_b(@id int)
returns varchar(1000)
as
begin
declare @s varchar(1000)
set @s=''
select @s=@s+' '+context from tb where id=@id
return (@s)
end
go
select id,dbo.fn_b(id) as context
from tb
group by id
/*
id context
---------------------------
1 a1 a2 a3
2 b1
3 c1 c2
*/
{
Console.WriteLine(item.key + item.value);
}
Console.Read();
}
public class Entity
{
public Entity(int Id, string value)
{
this.Id = Id;
this.Value = value;
}
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _value;
public string Value
{
get { return _value; }
set { _value = value; }
}
public override string ToString()
{
return this.Value.ToString();
}
}
}
[解决办法]
最后得加一个列转行的方法吧
[解决办法]