-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CountNumberOfMembersWhoseSurnameStartsWithEachLetterOfTheAlphabet.cs
63 lines (61 loc) · 2.72 KB
/
CountNumberOfMembersWhoseSurnameStartsWithEachLetterOfTheAlphabet.cs
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
55
56
57
58
59
60
61
62
63
using System.Linq;
using EFCorePgExercises.DataLayer;
using EFCorePgExercises.Utils;
using FluentAssertions;
namespace EFCorePgExercises.Exercises.StringOperations
{
[FullyQualifiedTestClass]
public class CountNumberOfMembersWhoseSurnameStartsWithEachLetterOfTheAlphabet
{
[FullyQualifiedTestMethod]
public void Test()
{
// https://pgexercises.com/questions/string/substr.html
// You'd like to produce a count of how many members you have whose surname starts with
// each letter of the alphabet. Sort by the letter, and don't worry about printing out a
// letter if the count is 0.
//
//select substr (mems.surname,1,1) as letter, count(*) as count
// from cd.members mems
// group by letter
// order by letter
EFServiceProvider.RunInContext(context =>
{
var members = context.Members
.Select(member => new { Letter = member.Surname.Substring(0, 1) })
.GroupBy(m => m.Letter)
.Select(g => new
{
Letter = g.Key,
Count = g.Count()
})
.OrderBy(r => r.Letter)
.ToList();
/*
SELECT SUBSTRING([m].[Surname], 0 + 1, 1) AS [Letter], COUNT(*) AS [Count]
FROM [Members] AS [m]
GROUP BY SUBSTRING([m].[Surname], 0 + 1, 1)
ORDER BY SUBSTRING([m].[Surname], 0 + 1, 1)
*/
var expectedResult = new[]
{
new { Letter = "B", Count = 5 },
new { Letter = "C", Count = 2 },
new { Letter = "D", Count = 1 },
new { Letter = "F", Count = 2 },
new { Letter = "G", Count = 2 },
new { Letter = "H", Count = 1 },
new { Letter = "J", Count = 3 },
new { Letter = "M", Count = 1 },
new { Letter = "O", Count = 1 },
new { Letter = "P", Count = 2 },
new { Letter = "R", Count = 2 },
new { Letter = "S", Count = 6 },
new { Letter = "T", Count = 2 },
new { Letter = "W", Count = 1 }
};
members.Should().BeEquivalentTo(expectedResult);
});
}
}
}