c# - Return one column values as list from stored procedure -
is possible return data structure directly mssql ?
public class myclass { public int id {get; set;} public int list<int> anotherids {get; set;} }
i need retrieve data list if id duplicate example: select * mytable
-------------------- | id | anthid | | 1 | 1 | | 1 | 2 | | 1 | 3 | | 2 | 1 | | 2 | 2 | | 2 | 3 | | 2 | 4 | --------------------
result list of 2 entities: myclass[0]{1, [1,2,3]} myclass[1]{2, [1,2,3,4]}
yes, possible. i'm including example can copy/paste query window , using example build sql return desired data:
declare @tbl table(id int, anotherid int) declare @aa varchar (200) declare @result table(id int, anotherids varchar(200)) set @aa = '' insert @tbl (id, anotherid) values(1,1) insert @tbl (id, anotherid) values(1,2) insert @tbl (id, anotherid)values(1,3) insert @tbl (id, anotherid) values(1,4) insert @tbl (id, anotherid) values(2,1) insert @tbl (id, anotherid) values(2,2) insert @tbl (id, anotherid) values(2,3) insert @tbl (id, anotherid) values(2,4) --select * @tbl declare @i int select @i = min(id) @tbl declare @max int select @max = max(id) @tbl while @i <= @max begin select @aa = coalesce (case when @aa = '' cast(anotherid varchar) else @aa + ',' + cast(anotherid varchar) end ,'') @tbl id=@i insert @result(id, anotherids) values(@i, @aa) set @aa='' set @i = @i + 1 end select * @result
result looks this:
id anotherids
1 1,2,3,4
2 1,2,3,4
Comments
Post a Comment