简体   繁体   中英

How to sort the result of stored procedure and return to js?

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

I want the result to show Game1 Game2 Game3

I did it like this so that it will sort the all 3 stored procedure in sequence.but it does not change in sequence. I also tried result.Sort() , but i also get the same result. is there any mistake that i did cause i try using order by in the stored procedure and it still won't works. Is there another method that you would suggest?

You need to create a new list from the sorted results. This should do the trick

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

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