使用存储过程作为搜索方法

时间:2018-11-14 08:24:49

标签: asp.net stored-procedures asp.net-core

我从ASP.Net Core 2.0开始,我试图通过搜索使用存储过程来重写方法GetAll。这是方法搜索:

public async Task<List<DepartmentTypeDto>> SearchDepartmentType()
{
    EnsureConnectionOpen();

    using (var command = CreateCommand("CM_DEPT_GROUP_Search", CommandType.StoredProcedure))
    {
        using (var dataReader = await command.ExecuteReaderAsync())
        {
                List<DepartmentTypeDto> result = new List<DepartmentTypeDto>();

                while (dataReader.Read())
                {
                    DepartmentTypeDto departmentTypeDto = new DepartmentTypeDto
                    {
                        GROUP_ID = dataReader["GROUP_ID"].ToString(),
                        GROUP_CODE = dataReader["GROUP_CODE"].ToString(),
                        GROUP_NAME = dataReader["GROUP_NAME"].ToString(),
                        NOTES = dataReader["NOTES"].ToString(),
                        RECORD_STATUS = dataReader["RECORD_STATUS"].ToString(),
                        MAKER_ID = dataReader["MAKER_ID"].ToString(),
                        CREATE_DT = Convert.ToDateTime(dataReader["CREATE_DT"]),
                        AUTH_STATUS = dataReader["AUTH_STATUS"].ToString(),
                        CHECKER_ID = dataReader["CHECKER_ID"].ToString(),
                        APPROVE_DT = Convert.ToDateTime(dataReader["APPROVE_DT"]),
                        AUTH_STATUS_NAME = dataReader["AUTH_STATUS_NAME"].ToString(),
                        RECORD_STATUS_NAME = dataReader["RECORD_STATUS_NAME"].ToString()
                    };
                }
                return result;   
            }
        }
}

这是服务:

public async Task<PagedResultDto<GetDepartmentTypeForView>> GetAll(GetAllDepartmentTypesInput input)
{
     var filteredDepartmentTypes = _departmentTypeRepository.SearchDepartmentType();

     var query = (from o in filteredDepartmentTypes
                  select new GetDepartmentTypeForView() { DepartmentType = ObjectMapper.Map<DepartmentTypeDto>(o) });

     var totalCount = await query.CountAsync();

     var departmentTypes = await query
            .OrderBy(input.Sorting ?? "departmentType.id asc")
            .PageBy(input)
            .ToListAsync();

     return new PagedResultDto<GetDepartmentTypeForView>(totalCount, departmentTypes);
}

但是我得到一个错误:

  

Task<List<DepartmentTypeDto>>不包含Select

的定义

有人知道我应该怎么做吗?我在Asp.Net Zero上工作。

1 个答案:

答案 0 :(得分:0)

我更改了搜索方式

public IQueryable<DepartmentTypeView> SearchDepartmentType(GetAllDepartmentTypesInput input, int top)
    {
        try
        {
            var GROUP_FILTER = input.Filter;
            var GROUP_CODE = input.GROUP_CODEFilter;
            var GROUP_NAME = input.GROUP_NAMEFilter;
            var AUTH_STATUS = input.AUTH_STATUSFilter;

            var result = Context.Query<DepartmentTypeView>().FromSql($"EXEC CM_DEPT_GROUP_Search @p_GROUP_FILTER = {GROUP_FILTER}, @p_GROUP_CODE={GROUP_CODE}, @p_GROUP_NAME={GROUP_NAME}, @p_AUTH_STATUS={AUTH_STATUS}, @p_TOP={top}");

            return result;
        }
        catch
        {
            return null;
        }
    }

在服务中,我调用该函数 var filteredDepartmentTypes = _departmentTypeRepository.SearchDepartmentType(input,100); 我还创建了新的类以保留结果,并且不要忘记用DTO类映射该类 configuration.CreateMap<DepartmentTypeView, DepartmentTypeDto>(); 它对我有用。