使用 table 别名和键入的查询选择多个

Selecting multiple with table alias and typed query

我需要加入同一个 table 两次,所以在文档中看到我可以使用 table 别名,但我在 select 加入 table 时遇到了一些困难]s..

这是我试过的:

var q = _AutoQuery.CreateDevXQuery(request, base.Request.GetRequestParams(), base.Request);
q.LeftJoin<WritingAssignment, Blog>((w, b) => w.WriterSuggestBlog == b.Id, Db.TableAlias("b1"));
q.LeftJoin<WritingAssignment, Blog>((w, b) => w.ApprovedBlog == b.Id, Db.TableAlias("b2"));

q.Select<WritingAssignment, WriterProfile, Blog>((wa, wp, b) => new
{
    WritingAssignment = wa,
    WriterProfile = wp,
    SuggestedBlog = Sql.TableAlias(b, "b1"),
    AcceptedBlog = Sql.TableAlias(b, "b2")
});

var values = Db.Select<Tuple<WritingAssignment, WriterProfile, Blog, Blog>>(q);

WritingAssignmentWriterProfile 都很好,但是这两个博客值只有默认的 属性 值。该文档仅显示了它使用单列的示例。用 table 别名引用整个 table 的方法是什么?

我用这个 select:

q.Select("writing_assignment.*, 0 EOT, writer_profile.*, 0 EOT, b1.*, 0 EOT, b2.*");

但我觉得我应该能够让它与 Sql.TableAlias 一起工作。这可能吗?

我刚刚在 this commit 中添加了对这种语法的支持,您的初始语法应该可以按预期工作:

q.Select<WritingAssignment, WriterProfile, Blog>((wa, wp, b) => new
{
    WritingAssignment = wa,
    WriterProfile = wp,
    SuggestedBlog = Sql.TableAlias(b, "b1"),
    AcceptedBlog = Sql.TableAlias(b, "b2")
});

var values = Db.Select<Tuple<WritingAssignment, WriterProfile, Blog, Blog>>(q);

默认情况下,如果未设置 EOT 分隔符,则在元组中选择多个 table 将根据模型字段数进行拆分,但只要所选字段列表与模型不匹配您可以使用 Sql.EOT 插入明确的 table 定界符,例如:

q.Select<WritingAssignment, WriterProfile, Blog>((wa, wp, b) => new
{
    Id = wa.Id,
    Name = wa.Name,
    t1 = Sql.EOT,
    WriterProfile = wp,
    t2 = Sql.EOT,
    SuggestedBlog = Sql.TableAlias(b, "b1"),
    t3 = Sql.EOT,
    AcceptedBlog = Sql.TableAlias(b, "b2")
});

var values = Db.Select<Tuple<WritingAssignment, WriterProfile, Blog, Blog>>(q);

此更改适用于 v5.11.1,即现在 available on MyGet