簡體   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#

這是我的代碼示例:

 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();
      }
}

我有多個 customobjectid 的多個數據行,我需要將這些數據添加到數組或列表中。 任何人都可以幫助我嗎?

這只是根據您的示例代碼在黑暗中拍攝的一個鏡頭,因為我對 dapper 一無所知(我假設這是 dapper?如果是這樣,請在您的問題中添加Dapper標簽),但看看是否像這讓你更接近你想要的。

我在調用QueryAsync時使用ToList() ,因此對於mapResult中的每個result ,都會返回一個新的List<rowData> 然后在.Select語句上調用.ToList()將結果返回為List<List<rowData>> ,其中每個內部列表是一行值,而外部列表是一個行列表。

下面的代碼將替換您的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM