2017-02-20 93 views
0

類:如何用fastcrud編寫sql代碼?

[Table("department_types", Schema = "public")] 
public class DepartmentTypes 


[Table("departments", Schema = "public")] 
    public class Department 

查詢:

private string BuildQuery() 
      { 
       return OrmConfiguration.GetSqlBuilder<DepartmentViewModel>().Format(
        [email protected]" SELECT {nameof(Department.Id):C}, 
           {nameof(Department.Name):C}, 
           {nameof(Department.TypeId):C}, 
           {nameof(DepartmentTypes.Name):C} as DepartmentTypeName 
         FROM {nameof(Department):T} 
         LEFT JOIN {nameof(DepartmentTypes):T} 
         ON {nameof(Department.TypeId):C} = {nameof(DepartmentTypes.Id):C}"); 
      } 

結果類:

public class DepartmentViewModel 
    { 
     public int Id { get; set; } 

     public string Name { get; set; } 

     public int TypeId { get; set; } 

     public string DepartmentTypeName { get; set; } 

    } 

我想和部和DepartmentTypes聯接,並得到一個DepartmentViewModel結果...

現在,而不是{nameof(DepartmentTypes):T}(對於實例mple)系統放入「DepartmentViewModel」,而不是DepartmentTypes的名稱,即:「department_types」

回答

0

我找到了答案:D。

private string BuildDepartmentListQuery() 
      { 
       var departmentTypeTable = Sql.Table<DepartmentType>(); 
       var departmentTable = Sql.Table<Department>(); 

       var departmentColumnId = Sql.TableAndColumn<Department>(nameof(Department.Id)); 
       var departmentColumnName = Sql.TableAndColumn<Department>(nameof(Department.Name)); 
       var departmentColumnTypeId = Sql.TableAndColumn<Department>(nameof(Department.TypeId)); 
       var departmentColumnManagerId = Sql.TableAndColumn<Department>(nameof(Department.ManagerId)); 

       var departmentTypeColumnId = Sql.TableAndColumn<DepartmentType>(nameof(DepartmentType.Id)); 
       var departmentTypeColumnName = Sql.TableAndColumn<DepartmentType>(nameof(DepartmentType.Name)); 

       string sqlBuilderResult = OrmConfiguration.GetSqlBuilder<Department>() 
        .Format(
         [email protected]"SELECT 
     {departmentColumnId} 
    , {departmentColumnName} 
    , {departmentColumnTypeId} 
    , {departmentColumnManagerId} 
    , {departmentTypeColumnName} as ""DepartmentTypeName"" 
    FROM {departmentTable} 
    LEFT JOIN {departmentTypeTable} 
    ON {departmentColumnTypeId} = {departmentTypeColumnId}"); 
       ; 

       return sqlBuilderResult; 
    } 
+0

如果它會更容易,我試了一下你的方式,然後只是做了我創建我自己的字符串插入原始SQL查詢。只要你有適合你的列的表格標識符,它仍然可以工作。此外,由於您聲明瞭作爲您的實體,因此您可以使用:T和:C項目來縮短某些表/列聲明。 –