如何对存储过程的结果进行排序并返回js?

时间:2018-08-27 00:15:44

标签: c# json sql-server

func create_user(email: String, password: String, completion: @escaping(_ res: Bool) -> Void ) {
  Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
    if let error = error {
      // Error creating new user.
      completion(false)
      print("In fail closure: \(create_suc)")
    } else {
      // Create successfully.
      completion(true)
    }
  }
}

create_user(email: acc_email.text!, password: acc_password.text!) { (res) in
   print(res)
}

我希望结果显示 游戏1 游戏2 游戏3

我这样做是为了让它按顺序对所有3个存储过程进行排序,但是不会按顺序进行更改。我也尝试过 if (gameName.Contains("Game1")) { using (var conn = new SqlConnection(SessionWrapper.BackOfficeSecondaryDbConnectionString)) { var query = @"crm_Game_category"; var param = new DynamicParameters(); param.Add("@DateFrom", dateFrom, dbType: DbType.DateTime); param.Add("@DateTo", dateTo, dbType: DbType.DateTime); param.Add("@Sort", 1); var GameReport = conn.Query<GameLogCategoryReportModel>(query, param, commandType: CommandType.StoredProcedure); if (GameReport.Count() > 0) { foreach (var GameLog in GameReport) { result.Add(GameLog); } } } } if (categoryName.Contains("Game2")) { using (var conn = new SqlConnection(SessionWrapper.BackOfficeSecondaryDbConnectionString)) { var query = @"crm_Game_CSOfficer"; var param = new DynamicParameters(); param.Add("@DateFrom", dateFrom, dbType: DbType.DateTime); param.Add("@DateTo", dateTo, dbType: DbType.DateTime); param.Add("@Sort", 2); var GameReport = conn.Query<GameLogCategoryReportModel>(query, param, commandType: CommandType.StoredProcedure); if (GameReport.Count() > 0) { foreach (var GameLog in GameReport) { result.Add(GameLog); } } } } if (categoryName.Contains("Game3")) { using (var conn = new SqlConnection(SessionWrapper.BackOfficeSecondaryDbConnectionString)) { var query = @"crm_CallLog_Game_FCR"; var param = new DynamicParameters(); param.Add("@DateFrom", dateFrom, dbType: DbType.DateTime); param.Add("@DateTo", dateTo, dbType: DbType.DateTime); param.Add("@Sort", 3); var GameReport = conn.Query<GameLogCategoryReportModel>(query, param, commandType: CommandType.StoredProcedure); if (GameReport.Count() > 0) { foreach (var GameLog in GameReport) { result.Add(GameLog); } } } } return JsonConvert.SerializeObject(result.OrderBy(x => x.Sort)); public class GameLogCategoryReportModel { public string Category { get; set; } public int Sort { get; set; } } ,但我也得到了相同的结果。是否有任何错误导致我尝试在存储过程中尝试使用order by,但仍然无法正常工作。您还有其他建议的方法吗?

1 个答案:

答案 0 :(得分:0)

您需要根据排序结果创建一个新列表。这应该可以解决问题

return JsonConvert.SerializeObject(result.OrderBy(x => x.Sort).ToList());