Linq2Db and string.join()



我在带有子查询的大查询中使用了Linq2db。在它里面的一个地方,我想使用字符串。连接():

...
FullPath = string.Join(" -> ", GetPathQuery(db, c.Id).Select(pi => pi.Name))
...

但我收到了一个例外:

LinqException: 'Join(" -> ", value(RI.达尔。Categories.AdminCategoryPreviewDAL)。GetPathQuery(value(RI.达尔。Categories.AdminCategoryPreviewDAL+<>c__DisplayClass4_0).db, c.Id).选择(pi => pi.名称))' 无法转换为 SQL。

我使用Postgre SQL,它具有非常适合我的concat_ws功能。所以我尝试使用它:

[Sql.Expression("concat_ws({1}, {0})")]
public static string JoinAsString(this IQueryable<string> query, string separator)
{
    return string.Join(separator, query);
}
...
FullPath = GetPathQuery(db, c.Id).Select(pi => pi.Name).JoinAsString(" -> ")
...

但我失败了,有同样的例外。


GetPathQuery 的完整源代码:

    private IQueryable<CategoryPathItemCte> GetPathQuery(IStoreDb db, Guid categoryId)
    {
        var categoryPathCte = db.GetCte<CategoryPathItemCte>(categoryHierarchy =>
        {
            return
                (
                    from c in db.Categories
                    where c.Id == categoryId
                    select new CategoryPathItemCte
                    {
                        CategoryId = c.Id,
                        ParentCategoryId = c.ParentId,
                        Name = c.Name,
                        SeoUrlName = c.SeoUrlName
                    }
                )
                .Concat
                (
                    from c in db.Categories
                    from eh in categoryHierarchy.InnerJoin(ch => ch.ParentCategoryId == c.Id)
                    select new CategoryPathItemCte
                    {
                        CategoryId = c.Id,
                        ParentCategoryId = c.ParentId,
                        Name = c.Name,
                        SeoUrlName = c.SeoUrlName
                    }
                );
        });
        return categoryPathCte;
    }

你可以这样尝试吗,

FullPath = string.Join(" -> ", GetPathQuery(db, c.Id).Select(pi => pi.Name).ToList());

更方便查询的方法

GetPathQuery(db, c.Id).Select(pi => pi.Name)
   .Aggregate(string.Empty, (results, nextString) 
               => string.Format("{0} -> {1}", results, nextString));

最新更新