【问题标题】:Map the result of stored procedure with the returning object using .net core使用 .net core 将存储过程的结果与返回对象映射
【发布时间】:2021-12-05 11:58:48
【问题描述】:

我是 .net 核心的新手。我从一个 Get API 调用开始,它将根据员工 ID 从 SP 返回单个员工记录。我有 2 个类,一个是 Employee,另一个是 Address。我在这两个表之间有一个组合关系。我想返回包含员工详细信息的员工对象以及地址列表。所以,我的 Employee 类看起来像这样:

public class Employee{
public string Name {get;set;}
....
public List<Address> Addr {get; set;}
}

我能够返回包含员工详细信息的员工对象。但是,Addr 返回 null。

这是我的代码:

 public async Task<Employee> GetEmployeeDetails(int employeeid)
    {
            Employee emp;
            var dictionary = new Dictionary<string, object>
                    {
                        {"@employeeid", employeeid }
                    };

            var parameters = new DynamicParameters(dictionary);

            using (IDbConnection con = await GetConnectionAsync(ConfigHandler.GetConnectionString(Constants.DefaultConnectionString)))
            {
                emp = (Employee)await ExecuteQueryFirstOrDefaultAsync<Employee>(con, "GetEmployeeDetails", parameters);
                con.Close();
            }
            return emp;
    }

我应该使用来自 SP 的多个结果集还是单个结果集?即员工的结果集和地址不同,或者我应该只返回一个结果集。因为我对第一个地址感兴趣。其次,如何将结果从 Sp 映射到对象。 我将不胜感激任何帮助/建议。 谢谢!

【问题讨论】:

  • 您好,您可以显示从您的 sql 查询返回的列吗?我的第一个建议是您需要知道 SQL 结果中的一行应该代表一个 C# 对象,SQL 没有树/节点的理解。所以一行是一个普通的对象。地址列表是一个节点。要填充它,您可以创建一个类 GetEmployeeDetails,它将代表 SQL 查询的确切结果,然后映射到您的 Employee
  • 嗨 @Max,我在 SP 中使用的查询是:从员工 e 左连接中选择 e.name、e.email、e.gender、a.addr、a.city、a.zip e.employeeid = a.employeeid 上的地址 a 其中 e.employeeId = @_employeeId

标签: c# asp.net-core asp.net-web-api


【解决方案1】:

你应该创建一个如下所示的类

    public class GetEmployeeDetailsResult 
    {
      public string Name {get;set;}
      public string Email {get;set;}
      public string Gender {get;set;}
      public string Addr {get;set;}
      public string City {get;set;}
      public string Zip {get;set;}
    }

在这个类中你可以有一个映射到Employee类的方法

public Employee MapEmployeeFromGetEmployeeDetailsResult(List<GetEmployeeDetailsResult> source)
{
  var firstElement = source.FirstOrDefault();
   return new Employee
            {
                Name = firstElement.Name,
                Email = firstElement.Email,
                Gender = firstElement.Gender,
                //this line will iterate and create the node for addresses
                Addr = source                  
                    .Select(emp => new Address
                    {
                        Addr = emp.Addr, 
                        City = emp.City,
                        Zip = emp.Zip
                    })
                    .ToList()
            };
}

然后使用这个类作为你的 SQL 查询的返回类型

         public async Task<Employee> GetEmployeeDetails(int employeeid)
            {
                    Employee emp;
                    var dictionary = new Dictionary<string, object>
                            {
                                {"@employeeid", employeeid }
                            };
        
                    var parameters = new DynamicParameters(dictionary);
        
                    using (IDbConnection con = await GetConnectionAsync(ConfigHandler.GetConnectionString(Constants.DefaultConnectionString)))
                    {
                        var result = await ExecuteQueryFirstOrDefaultAsync<GetEmployeeDetailsResult>(con, "GetEmployeeDetails", parameters);
                        con.Close();
                        emp = new GetEmployeeDetailsResult()
                              .MapEmployeeFromGetEmployeeDetailsResult(result);
                    }
                    return emp;    
    }

如果您不想要 new GetEmployeeDetailsResult() .MapEmployeeFromGetEmployeeDetailsResult(result); 行,可以将方法 MapEmployeeFromGetEmployeeDetailsResult 改为 static 并更改

new GetEmployeeDetailsResult().MapEmployeeFromGetEmployeeDetailsResult(result);

GetEmployeeDetailsResult.MapEmployeeFromGetEmployeeDetailsResult(result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多