简短的回答是肯定的,您可以检查查询实例并遍历选定的列,如下所示:
var columns = query.GetComponents<AbstractColumn>("select");
foreach(var column in columns)
{
if(column is Column col)
{
Console.WriteLine(col.Name);
} elseif(column is RawColumn raw) {
Console.WriteLine(col.Expression);
} elseif(column is QueryColumn subQuery) {
// do something with subQuery.Query
}
}
但我建议在不选择任何列的情况下构建基本查询,并在稍后阶段仅选择所需的列。
请注意,在编译阶段,如果您不选择任何列 SqlKata 将把它翻译成这对select *
您来说很方便。
var baseQuery = new Query("TableOrViewHere as Report")
// at a later phase for example in the controller
// select the columns dynamically
var innerQuery = baseQuery.Clone().Select(Request.Columns);
var data = db.Query()
.From(innerQuery.As("inner"))
.Get();