问题描述
我有一个博客条目表,一个标签表和一个将标签与博客条目相交的表.
I have a table of blog entries, a table of tags, and a table that intersects the tags to a blog entry.
我想将博客条目的标签汇总到以逗号分隔的字符串中,以在同一结果集中返回.这就是我在SQL中完成的方式:
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
我正在使用xml路径来汇总标签,并且正在寻找使用LINQ进行此操作的等效方法.
I'm using for xml path to roll my tags up and I'm looking for the equivalent way to do this with LINQ.
推荐答案
这是LINQ to SQL不能亮的那些情况之一.
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.
这篇关于是否有"for xml path"?相当于LINQ to SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!