locked
How to return concatenated strings with a query

    Question

  • Let's say I have this relationship:  A Person has zero or many Aliases.  How can I query the database to return a set of Persons with all their Aliases concatenated as a single string?

     

    For instance, if Peter's aliases are "Pedro", "Simon", "Simeon" and "Pete", I'd like to return the following:

     

    "Pedro, Simon, Simeon, Pete"

     

    I know I can query for the list of Aliases and concatenate them locally, but I'd rather do it in the query.  Is this possible?  I was thinking of writing a custom Aggregate(),  Is that the way to go?

     

     

     

     

     

    Thursday, February 07, 2008 5:24 AM

Answers

  • Here is how to do it:

    Code Snippet

    from person in Person
    select new {
      Name = person.Name
      Aliases = String.Join(",", person.Aliases.Select(a => a.AliasText).ToArray())
    }


    Thursday, February 07, 2008 10:51 PM

All replies

  • Here is how to do it:

    Code Snippet

    from person in Person
    select new {
      Name = person.Name
      Aliases = String.Join(",", person.Aliases.Select(a => a.AliasText).ToArray())
    }


    Thursday, February 07, 2008 10:51 PM
  • right on!  Thanks.

     

    Friday, February 08, 2008 3:58 AM