ServiceStack JOIN 3表

时间:2014-10-25 17:11:56

标签: c# sqlite servicestack ormlite-servicestack

可以使用ServiceStack加入2个以上的表吗? 我试过类似的东西,但我需要链接evrything:

// Join PatientDetails and BedDetails
SqlExpression<PatientDetails.PatientDetails> q = _dbConnection.From<PatientDetails.PatientDetails>();
q.Join<PatientDetails.PatientDetails, BedDetails.BedDetails>((patient, bed) => patient.ByBedId == bed.BedDetailsId);
List<PatientDetails.PatientDetails> PatientBedJOIN = _dbConnection.Select(q);

// Join PatientSession and PatientDetails
SqlExpression<PatientSession> q1 = _dbConnection.From<PatientSession>();
q1.Join<PatientSession, PatientDetails.PatientDetails>((session, patientd) => session.ByPatientId == patientd.PatientDetailsId);
List<PatientSession> SessionPatientJOIN = _dbConnection.Select(q1);

我使用OrmLite和Sqlite, 感谢

1 个答案:

答案 0 :(得分:4)

OrmLite的Advanced Join Example显示了如何继续向同一查询添加联接,例如:

List<FullCustomerInfo> rows = db.Select<FullCustomerInfo>(  // Map results to FullCustomerInfo POCO
  db.From<Customer>()                                       // Create typed Customer SqlExpression
    .LeftJoin<CustomerAddress>()                            // Implicit left join with base table
    .Join<Customer, Order>((c,o) => c.Id == o.CustomerId)   // Explicit join and condition
    .Where(c => c.Name == "Customer 1")                     // Implicit condition on base table
    .And<Order>(o => o.Cost < 2)                            // Explicit condition on joined Table
    .Or<Customer,Order>((c,o) => c.Name == o.LineItem));    // Explicit condition with joined Tables

您的示例中的内容如下:

var q = _dbConnection.From<PatientDetails.PatientDetails>()
    .Join<PatientDetails.PatientDetails, BedDetails.BedDetails>(
        (patient, bed) => patient.ByBedId == bed.BedDetailsId)
    .Join<PatientDetails.PatientDetails, PatientSession>(
        (patientd, session) => session.ByPatientId == patientd.PatientDetailsId);

var SessionPatientJOIN = _dbConnection.Select(q);