Is there a “for xml path” equivalent in LINQ to SQ

2019-06-28 05:13发布

问题:

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.

回答1:

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.