1

类:

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


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

询问:

private string BuildQuery()
            {
                return OrmConfiguration.GetSqlBuilder<DepartmentViewModel>().Format(
                    $@" 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; }

    }

我想加入 Department 和 DepartmentTypes 并获得 DepartmentViewModel 结果...

现在代替{nameof(DepartmentTypes):T}(例如)系统放置“DepartmentViewModel”,而不是 DepartmentTypes 的名称,即:“department_types”

4

1 回答 1

1

我找到了答案: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(
                        $@"SELECT
      {departmentColumnId}
    , {departmentColumnName}
    , {departmentColumnTypeId}
    , {departmentColumnManagerId}
    , {departmentTypeColumnName} as ""DepartmentTypeName""
    FROM {departmentTable}
    LEFT JOIN {departmentTypeTable}
    ON {departmentColumnTypeId} = {departmentTypeColumnId}");
                ;

                return sqlBuilderResult;
    }
于 2017-02-24T13:55:12.293 回答