0

我有以下查询,我想在 Linq 中写这个(使用 c#)。我该怎么做?

declare @parentId bigint
set @parentId = (select ArticlePart from tbl_Article where ArticleID = 70001)
declare @ids varchar(100)
set @ids=''
while @parentId is not null
begin
    set @ids = @ids +CAST( @parentId as varchar(50))+','
    set @parentId = (select ArticlePart from tbl_Article where ArticleID = @parentId)
end
4

1 回答 1

1

并不是说它可能是解决问题的最佳方法,而是回答这个问题:

var ids = string.Join(",", ArticleQueriable(articles, 70001));


Queriable<int> ArticleQueriable(Queriable<Article> tbl_Article, int parentID)
{
    while (true)
    {
        var parent = tbl_Article.SingleOrDefault(a => a.ArticleID == parentID);

        if (parent != null)
        {
            parentID = parent.ArticlePart.Dump();
            yield return parentID;
        }
        else
        {
            yield break;
        }
    }
}
于 2013-05-19T22:25:24.200 回答