简体   繁体   English

I have custom data which is return from stored procedure so I need to add those data into list<> and return in asp.net core web api c#

[英]I have custom data which is return from stored procedure so I need to add those data into list<> and return in asp.net core web api c#

Here is my code example:这是我的代码示例:

 using (IDbConnection con = new SqlConnection(Constants.DefaultConnectionString))
 {
      if (con.State == ConnectionState.Closed)
          con.Open();

      DynamicParameters parameter = new DynamicParameters();
      parameter.Add("@StandardObjectName", StandardObject);
      parameter.Add("@UserID", UserID);

      var AccountRecord = await con.QueryAsync<GetCustomDataForStandardObject>(
           StoredProcedure.GetCustomobjectDataForStandardObject, parameter, 
           commandType: CommandType.StoredProcedure);

      var mapResult = Common.AutoMapper.MapListData<GetCustomDataForStandardObject, 
           GetStandardBYName>(AccountRecord.ToList());

      List<string> CustomData = new List<string>();
      GetCustomObjectLinkData Data = null;

      // DataTable dt = new DataTable();
      // LeadRecord.ToList();

      for (int a = 0; a < mapResult.Count; a++)
      {
           int customobjectid = 0;
           customobjectid = mapResult[a].CustomObjectID;
           DynamicParameters pa = new DynamicParameters();
           pa.Add("@CustomObjectSchemaId", customobjectid);
           var rowAffected = await con.QueryAsync(StoredProcedure.GetCustomObjectData, 
               pa, commandType: CommandType.StoredProcedure);
           rowAffected.ToList();
      }
}

I have multiple data rows for multiple customobjectid and I need to add those data into an array or list.我有多个 customobjectid 的多个数据行,我需要将这些数据添加到数组或列表中。 Can anyone help me, please?任何人都可以帮助我吗?

This is just a shot in the dark based on your sample code, since I don't know anything about dapper (I'm assuming this is dapper? If so, please add the Dapper tag to your question), but see if something like this gets you closer to what you want.这只是根据您的示例代码在黑暗中拍摄的一个镜头,因为我对 dapper 一无所知(我假设这是 dapper?如果是这样,请在您的问题中添加Dapper标签),但看看是否像这让你更接近你想要的。

I'm using ToList() on the call to QueryAsync , so for each result in mapResult , a new List<rowData> is returned.我在调用QueryAsync时使用ToList() ,因此对于mapResult中的每个result ,都会返回一个新的List<rowData> Then calling .ToList() on the .Select statement returns the result as a List<List<rowData>> , where each inner list is a row of values, and the outer list is a list of rows.然后在.Select语句上调用.ToList()将结果返回为List<List<rowData>> ,其中每个内部列表是一行值,而外部列表是一个行列表。

The code below would replace your for loop:下面的代码将替换您的for循环:

var rowsAffected = mapResult.Select(result =>
    {
        DynamicParameters pa = new DynamicParameters();
        pa.Add("@CustomObjectSchemaId", result.CustomObjectID);
        return await con.QueryAsync(StoredProcedure.GetCustomObjectData, 
            pa, commandType: CommandType.StoredProcedure).ToList();
    }).ToList();

// rowsAffected is a List<List<rowData>>, where the outer list contains all the rows
// and each inner list is the row data for a specific row

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从存储过程返回数据的对象返回列表C# - Return List of Objects from stored procedure return data C# 我需要在 ASP.Net 核心的验证属性中返回自定义验证结果(响应) Web API - I need to return customized validation result (response) in validation attributes in ASP.Net core Web API 如何使用 ZD7EFA19FBE7D3972FD5ADB6024Z2 在 ASP.NET Core 6 Web API 中运行存储过程? - How run stored procedure in ASP.NET Core 6 Web API using C#? 如何使用asp.net读取没有参数的存储过程返回的日期? - How to read the date that is return from a stored procedure which have no parameter using asp.net? Call MySql stored procedure which take 2 parameters from asp.net core 2.2 web API controller - Call MySql stored procedure which take 2 parameters from asp.net core 2.2 web API controller ASP.Net Core - 实体框架 - 调用没有返回数据的存储过程(在 void 方法中) - ASP.Net Core - Entity Framework - Call Stored Procedure With No Return Data (in a void method) 如何从 ASP.Net Core Web API Controller 中的数据库返回关系数据 - How to return a relational data from DB in ASP.Net Core Web API Controller Asp.net Web API - 从 actionfilter 返回数据 - Asp.net Web API - return data from actionfilter 用于存储过程的ASP.NET Web Api控制器不返回数据 - ASP.NET Web Api controller for stored procedure not returning data 从存储过程中获取C#asp.net中的返回值(语法问题) - Getting a Return Value in C# asp.net from a stored procedure (syntax issue)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM