Billy Coover
Billy Coover

Reputation: 3837

Is there a "for xml path" equivalent in LINQ to SQL?

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.

Upvotes: 4

Views: 2356

Answers (1)

Andrew Hare
Andrew Hare

Reputation: 351456

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.

Upvotes: 4

Related Questions