2017-07-07 42 views
1

我有一個Customer類具有以下屬性:查詢到一個複雜的對象,具有小巧玲瓏

public int Id { get; set; } 
public string Name { get; set; } 
public int AddressId { get; set; } 
public Address Address { get; set; } 

我的目標是編寫將使用一個內連接到每個客戶內填充整個地址屬性短小精悍查詢這是返回。

這裏是我有什麼,它是工作,但我想知道如果這是這樣做的乾淨/簡單的方法:我有添加其它財產,如有些擔心

StringBuilder sql = new StringBuilder(); 
using (var conn = GetOpenConnection()) 
{ 
    sql.AppendLine("SELECT c.Id, c.Name, c.AddressId, a.Address1, a.Address2, a.City, a.State, a.ZipCode "); 
    sql.AppendLine("FROM Customer c "); 
    sql.AppendLine("INNER JOIN Address a ON c.AddressId = a.Id "); 

    return conn.Query<Customer, Address, Customer>(
     sql.ToString(), 
     (customer, address) => { 
      customer.Address= address; 
      return userRole; 
     }, 
     splitOn: "AddressId" 
    ).ToList(); 
} 

public Contact Contact { get; set; } 

我不知道如何切換上面的語法來填充地址和聯繫人。

+0

訣竅是讓分割的列具有相同的名稱(如'Id'),然後它只是'Query '。 – juharr

回答

1

我使用的版本小巧玲瓏的1.40版本,我寫這篇文章的方式,沒有問題填充不止一個對象,但我得到了使用填充的Maximo表是八

public class Customer { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public int AddressId { get; set; } 
     public int ContactId { get; set; } 
     public Address Address { get; set; } 
     public Contact Contact { get; set; } 
    } 

    public class Address { 
     public int Id { get; set; } 
     public string Address1 {get;set;} 
     public string Address2 {get;set;} 
     public string City {get;set;} 
     public string State {get;set;} 
     public int ZipCode {get;set;} 
     public IEnumerable<Customer> Customer {get;set;} 
    } 

    public class Contact { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public IEnumerable<Customer> Customer {get;set;} 
    } 

    using (var conn = GetOpenConnection()) 
    { 
     var query = _contextDapper 
      .Query<Customer, Address, Contact, Customer>([email protected]" 
       SELECT c.Id, c.Name, 
        c.AddressId, a.Id, a.Address1, a.Address2, a.City, a.State, a.ZipCode, 
        c.ContactId, ct.Id, ct.Name 
       FROM Customer c 
       INNER JOIN Address a ON a.Id = c.AddressId 
       INNER JOIN Contact ct ON ct.Id = c.ContactId", 
       (c, a, ct) => 
       { 
        c.LogType = a; 
        c.Contact = ct; 
        return c; 
       }, splitOn: "AddressId, ContactId") 
      .AsQueryable(); 

     return query.ToList();   
    } 
+0

幫了我很多:P謝謝! :)我也在這裏用我的例子 –

0

採取看看我的例子與一個大的查詢,請注意,每個查詢線這是一個不同的對象。

public List<Appointment> GetList(int id) 
{ 
    List<Appointment> ret; 
    using (var db = new SqlConnection(connstring)) 
    { 
     const string sql = @"SELECT AP.[Id], AP.Diagnostics, AP.Sintomns, AP.Prescription, AP.DoctorReport, AP.AddressId, 
     AD.Id, AD.Street, AD.City, AD.State, AD.Country, AD.ZIP, Ad.Complement, 
     D.Id, D.Bio, d.CRMNumber, D.CRMNumber, D.CRMState, 
     P.Id, 
     S.Id, S.Name, 
     MR.Id, MR.Alergies, MR.BloodType, MR.DtRegister, Mr.HealthyProblems, MR.HealthyProblems, MR.Height, MR.MedicalInsuranceNumber, MR.MedicalInsuranceUserName, MR.Medications, MR.Weight, 
     MI.Id, MI.Name 
     from Appointment AP 
     inner join [Address] AD on AD.Id = AP.AddressId 
     inner join Doctor D on D.Id = AP.DoctorId 
     inner join Patient P on P.Id = AP.PatientId 
     left join Speciality S on S.Id = D.IDEspeciality 
     left join MedicalRecord MR on MR.Id = P.MedicalRecordId 
     left join MedicalInsurance MI on MI.Id = MR.MedicalInsuranceId 
     where AP.Id = @Id 
     order by AP.Id desc"; 

     ret = db.Query<Appointment, Address, Doctor, Patient, Speciality, MedicalRecord, MedicalInsurance, Appointment>(sql, 
      (appointment, address, doctor, patient, speciality, medicalrecord, medicalinsurance) => 
      { 
       appointment.Address = address; 
       appointment.Doctor = doctor; 
       appointment.Patient = patient; 
       appointment.Doctor.Speciality = speciality; 
       appointment.Patient.MedicalRecord = medicalrecord; 
       appointment.Patient.MedicalRecord.MedicalInsurance = medicalinsurance; 
       return appointment; 
      }, new { Id = id }, splitOn: "Id, Id, Id, Id, Id, Id").ToList(); 
    } 
    return ret; 
}