Dapper(C#のMicroOrm)でSUMを実行する方法です。
/// <summary>
/// ユーザーのバトルの合計スコアを取得する
/// </summary>
/// <returns>スコア</returns>
public uint SumBattleScore(ulong userId)
{
IDbConnection conn = /* 何かしらの方法で取得する */;
string sql = "SELECT SUM(score) FROM battle_result WHERE user_id = @UserId";
return Convert.ToUInt32(conn.ExecuteScalar(sql, new { UserId = userId }));
// ExecuteScalar が使えることを教えてもらいました
/*
uint? data = conn.QueryFirst<uint?>(sql, new { UserId = userId });
return (data == null) ? 0 : data.Value;
*/
}
データが何も取得できないときはNullになるみたいなので、Nullable型で取得してあげる必要あるみたい。