Hi all,
I have a problem and need help to address this urgent requirement.The data in the database table resembles like the as below.
| ID | Name |
| 1001 | john |
| 1002 | davis |
| 1003 | name |
| 1002 | name |
| 1001 | johny |
| 1003 | mani |
| 1003 | ganesh |
I need to show the string in the table like below using c# linq
| ID | Name |
| 1001 | John, johny |
| 1002 | Davis, name |
| 1003 | Name, mani, ganesh |
or
1001 = john, Johnny
1002 = davis, name
1003 = name, mani, ganesh
Any kind of solution to this problem is greatly appreciated.
Thanks in advance.
Regards,
Rajesh
Jignesh TrivediPosted Jan 25, 2012, 12:51 AM
you can also doing same thing using Lamba expression
try
List persons = new List();
persons.Add(new Person { ID = 1001, Name = "john" });
persons.Add(new Person { ID = 1002, Name = "davis" });
persons.Add(new Person { ID = 1003, Name = "name" });
persons.Add(new Person { ID = 1002, Name = "name" });
persons.Add(new Person { ID = 1001, Name = "johny" });
persons.Add(new Person { ID = 1003, Name = "mani" });
persons.Add(new Person { ID = 1003, Name = "ganesh" });
var test = from p in persons
group p by new { p.ID } into a
select new
{
ID = a.Key.ID,
Name = string.Join(",", persons.Where(f => f.ID == a.Key.ID).Select(g => g.Name).ToArray())
};
hope this help.
VulpesPosted Jan 24, 2012, 3:19 PM