-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlLiteStatService.cs
More file actions
54 lines (42 loc) · 1.34 KB
/
SqlLiteStatService.cs
File metadata and controls
54 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
namespace Backend
{
public class SqlLiteStatService : IStatService
{
private readonly SqliteDbManager db;
public SqlLiteStatService(SqliteDbManager db)
{
this.db = db;
}
public List<Tuple<string, int>> GetCountryPopulations()
{
var query = @"
SELECT country.CountryName,
sum(city.population)
FROM country
LEFT JOIN state ON country.CountryId = state.CountryId
LEFT JOIN city ON state.StateId = city.StateId
GROUP BY country.CountryName";
var dt = db.GetDataByQuery(query);
var result = new List<Tuple<string, int>>();
foreach (var row in dt.Rows)
{
var pair = ((DataRow)row).ItemArray;
string country = pair[0].ToString();
if (!int.TryParse(pair[1].ToString(), out var population))
{
throw new FormatException("Invalid population data");
}
result.Add(new Tuple<string, int>(country, population));
}
return result;
}
public async Task<List<Tuple<string, int>>> GetCountryPopulationsAsync()
{
return await Task.FromResult<List<Tuple<string, int>>>(GetCountryPopulations());
}
}
}