concatenate rows

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
Posts: 3
Joined: Tue 28 Jul 2009 09:34

concatenate rows

Post by actuary » Tue 28 Jul 2009 09:37

Hi all,

I have written a bit of linq where it gets data from the db in the form of

Id Name Category
1 Name1 Cat1
2 Name2 Cat1
3 Name1 Cat3

I am trying to merge Category column for same Ids, something like:

Id Name Category
1 Name1 Cat1, Cat3
2 Name2 Cat1

How is this possible in LINQ? My statement is

var query = from p in table
select new {p.Id, p.Name, p.Category}

Any help would be appreciated.


Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Tue 28 Jul 2009 12:57

Here is one of possible solutions:

Code: Select all

        List ncs = new List();
        foreach (var el in from it in db.Namecategories group it by it.Name) {
          string categories = null;
          foreach (var cat in el) {
            if (categories != null)
              categories += ", ";
            categories += cat.Category;
          ncs.Add(new Namecategory
            Name = el.Key,
            Category = categories

Post Reply