使用OrmLite如何通过多列将表联接?
我有Notes
表,该表可以具有QutoeHeader
或OrderHeader
的数据,所以我试图像这样加入并使用SelectMulti()
:
var userSpecificQuery =
_db
.From<Customer>()
.Where(x => x.SalesRepresentativeId == userId)
.LeftJoin<Customer, QuoteHeader>((c, q) => c.Id == q.CustomerId)
.LeftJoin<Customer, OrderHeader>((c, o) => c.Id == o.CustomerId)
.LeftJoin<OrderHeader, QuoteHeader, Notes>((oh,qh, n) => oh.Id == n.OrderId || qh.Id == n.QuoteId)
;
var userSpecificRecords =
_db.SelectMulti<Customer, QuoteHeader, OrderHeader, Notes>(userSpecificQuery);
但是我得到错误:
多次指定表名“ quote_header”
我想加入所有与OrderId或QuoteId匹配的记录。我尝试了几种不同的方法,并得到相同的错误。这可能吗?
编辑:
这里是音符模型
public class Notes : BaseModel
{
[AutoIncrement]
public long Id { get; set; }
[ForeignKey(typeof(QuoteHeader))]
public long? QuoteId { get; set; }
[ForeignKey(typeof(OrderHeader))]
public long? OrderId { get; set; }
public string NoteText { get; set; }
}
不是将QuoteHeader
多次连接,而是将条件移到Where
:
db.From<Customer>()
.Where(x => x.SalesRepresentativeId == userId)
.LeftJoin<Customer, QuoteHeader>((c, q) => c.Id == q.CustomerId)
.LeftJoin<Customer, OrderHeader>((c, o) => c.Id == o.CustomerId)
.LeftJoin<OrderHeader, Notes>((oh, n) => oh.Id == n.OrderId)
.Where<OrderHeader,QuoteHeader,Notes>((oh,qh,n) => oh.OrderId != null || qh.Id == n.QuoteId)