4

I have a table of blog entries, a table of tags, and a table that intersects the tags to a blog entry.

I want to roll-up the tags of a blog entry into a comma delimited string to be returned in the same result set. This is how I've done it in SQL:

select 
    be.Title
    ,Tags = lower((
        select
            stuff((
                select distinct 
                    ',' + bc.Category
                from    
                    BlogEntryCategory bec
                    join BlogCategory bc on bc.BlogCategory_ID = bec.BlogCategory_ID
                where    
                    bec.BlogEntry_ID = be.BlogEntry_ID
                for xml path('')),1,1,'')        
        )
    )
from 
    BlogEntry be

I'm using for xml path to roll my tags up and I'm looking for the equivalent way to do this with LINQ.

Billy Coover
  • 3,827
  • 5
  • 36
  • 50
  • You can see this post. It saved my time http://stackoverflow.com/questions/30044945/what-is-the-equivalent-of-xml-path-and-stuff-in-linq-lambda-expression-group-co – fxdoanhuy Sep 09 '15 at 04:15

1 Answers1

4

This is one of those situations where LINQ to SQL does not shine.

This code should be left in the database - it will be easier maintained and most likely will perform better there. Is there a particular reason you want to move this working code out of the database and into your application code? If it were up to me I would leave this code where it is.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • I inherited a SQL2000 blog database and am trying to mimic some of the queries that I have on my own sql2005 server. The "for xml path" is not supported on SQL2000 so I thought I could use LINQ to SQL to fill that gap. – Billy Coover Jan 02 '10 at 14:20