[英]Linq2Db and string.join()
I am using Linq2db in my big query with subqueries.我在带有子查询的大查询中使用 Linq2db。 At the one place inside it, I want to use string.Join():
在其中的一个地方,我想使用 string.Join():
...
FullPath = string.Join(" -> ", GetPathQuery(db, c.Id).Select(pi => pi.Name))
...
But I have received an exception:但我收到了一个例外:
LinqException: 'Join(" -> ", value(RI.DAL.Categories.AdminCategoryPreviewDAL).GetPathQuery(value(RI.DAL.Categories.AdminCategoryPreviewDAL+<>c__DisplayClass4_0).db, c.Id).Select(pi => pi.Name))' cannot be converted to SQL.
LinqException: 'Join(" -> ", value(RI.DAL.Categories.AdminCategoryPreviewDAL).GetPathQuery(value(RI.DAL.Categories.AdminCategoryPreviewDAL+<>c__DisplayClass4_0).db, c.Id).Select(pi => pi .Name))' 无法转换为 SQL。
I use Postgre SQL and it has the concat_ws
function which is perfect for me.我使用 Postgre SQL,它有
concat_ws
函数,这对我来说是完美的。 So I try to use it:所以我尝试使用它:
[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(" -> ")
...
But I was failed with the same exception.但由于同样的例外,我失败了。
The full source code of the GetPathQuery: 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;
}
can you try like this,你可以这样试试吗
FullPath = string.Join(" -> ", GetPathQuery(db, c.Id).Select(pi => pi.Name).ToList());
More query friendly approach更友好的查询方法
GetPathQuery(db, c.Id).Select(pi => pi.Name)
.Aggregate(string.Empty, (results, nextString)
=> string.Format("{0} -> {1}", results, nextString));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.