我有以下查询,我想在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
答案 0 :(得分: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;
}
}
}