Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq2Db和string.join()_C#_Linq2db - Fatal编程技术网

C# Linq2Db和string.join()

C# Linq2Db和string.join(),c#,linq2db,C#,Linq2db,我在带有子查询的大查询中使用Linq2db。 在其中的一个位置,我想使用string.Join: ... FullPath = string.Join(" -> ", GetPathQuery(db, c.Id).Select(pi => pi.Name)) ... 但我收到了一个例外: LinqException:'Join->,valueRI.DAL.Categories.AdminCategorityPreviewDal.GetPathQueryvalueRI.DAL.Cat

我在带有子查询的大查询中使用Linq2db。 在其中的一个位置,我想使用string.Join:

...
FullPath = string.Join(" -> ", GetPathQuery(db, c.Id).Select(pi => pi.Name))
...
但我收到了一个例外:

LinqException:'Join->,valueRI.DAL.Categories.AdminCategorityPreviewDal.GetPathQueryvalueRI.DAL.Categories.AdminCategorityPreviewDal+c_uu显示类4_0.db,c.Id.Selectpi=>pi.Name'无法转换为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));

这不是一个好主意。如果我没弄错的话,一个SQL查询将被分成几个查询+C代码。我认为性能会下降。我添加了另一个备选方案,您能检查一下吗?我已经将string.Format替换为如下简单的串联:results+->+nextString,因为linq2db无法使用string.Format。问题是它很奇怪,但仍然不起作用:LinqException:Sequence'valueRI.DAL.Categories.AdminCategorityPreviewDal.GetPathQueryvalueRI.DAL.Categories.AdminCategorityPreviewDal+c_uDisplayClass4_0.db,c.Id.Selectpi=>pi.Name.AggregateString.Empty,results,nextString=>results+->+nextString'无法转换为SQL。我的完整代码是:FullPath=GetPathQuerydb,c.Id.Selectpi=>pi.Name.Aggregatestring.Empty,results,nextString=>results+->+nextString是否可以尝试使用simple select+aggreate?我想看看这个问题是否与聚合有关。你能提供GetPathQuery方法的源代码吗?当然,我已经将它添加到了文章正文中。