2009-06-01 38 views
4

对不起,如果问题重复,问题的错误标题很抱歉。Linq to SQL使用分隔符追加多个记录

我有两个数据库表:

Users     Documents 
-------    --------- 
ID     ID 
Name     DocumentName 
         UserID 

说我有Users

1, "bob" 

和三个相关记录Documents

1, "Doc1", 1 
2, "Doc2", 1 
3, "Doc3", 1 

1个记录我想产生一个结果设置:

1, "bob", "Doc1, Doc2, Doc3" 

我已经试过各种事情,涉及合并的多个结果集,但得到的错误:Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.

我应该如何去这样做。

回答

5

或者:

  • 采取列 “是” 从服务器做CONCAT在C#
  • 写,做它的SP,并通过LINQ

拨打SP SP(接受@UserID)可以这样做:

DECLARE @txt varchar(max) 
SET @txt = '' 
SELECT @txt = @txt + [DocumentName] + ', ' 
FROM [Documents] 
WHERE [UserID] = @UserID 

SELECT [ID], [Name], @txt AS [Docs] 
FROM [Users] 
WHERE [ID] = @UserID