c# - Linq2Db and string.join()
问题描述
I am using Linq2db in my big query with subqueries. At the one place inside it, I want to use 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.
I use Postgre SQL and it has the concat_ws
function which is perfect for me. 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:
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));
推荐阅读
- firefox - 如何将 Firefox 小书签转换为网络扩展或添加?
- excel - 如何在 vba 中使用 Vlookup
- ggplot2 - 在 ggplot 中,如何在 barplot 上制作 panel.grid.major?
- javascript - 如何让 jQuery 在 ES6 文件中正常工作
- node.js - Nodejs - 如何在 ubuntu 中卸载旧版本的 nodejs 并安装新版本的 nodejs
- swift - 如何使用 Stepper 将数据从 VC 传递到嵌入式 TableView
- python-3.x - 如何修复错误“未定义大文本”?,我不确定我是否打算在代码开头定义它
- node.js - 如何在反应中显示base64图像?
- python - 由于 User-Agent 问题导致 AIOHTTP 客户端超时
- mobile - SQLite 错误没有这样的表:resco 现场服务移动应用程序中的活动指针