简体   繁体   中英

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. 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.

I'm using ToList() on the call to QueryAsync , so for each result in mapResult , a new List<rowData> is returned. 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.

The code below would replace your for loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM