-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (90 loc) · 4.05 KB
/
Program.cs
File metadata and controls
98 lines (90 loc) · 4.05 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Microsoft.Extensions.Logging;
namespace Blog {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
// Configure LoggerFactory to suppress detailed logs
var loggerFactory = LoggerFactory.Create(builder => {
builder.AddConsole();
builder.SetMinimumLevel(LogLevel.Warning);
});
var context = new MyDbContext(loggerFactory);
context.Database.EnsureCreated();
InitializeData(context);
// Query 1: How many comments each user left
Console.WriteLine("How many comments each user left:");
var commentsPerUser = context.BlogPosts
.SelectMany(post => post.Comments)
.GroupBy(comment => comment.UserName)
.Select(group => new {
UserName = group.Key,
CommentCount = group.Count()
})
.ToList();
foreach (var result in commentsPerUser) {
Console.WriteLine($"{result.UserName}: {result.CommentCount}");
}
// Query 2: Posts ordered by date of last comment
Console.WriteLine("Posts ordered by date of last comment:");
var postsOrderedByLastComment = context.BlogPosts
.Select(post => new {
PostTitle = post.Title,
LastCommentDate = post.Comments.Max(comment => comment.CreatedDate)
})
.OrderByDescending(post => post.LastCommentDate)
.ToList();
foreach (var result in postsOrderedByLastComment) {
Console.WriteLine($"{result.PostTitle}: '{result.LastCommentDate:yyyy-MM-dd}'");
}
// Query 3: How many last comments each user left
Console.WriteLine("How many last comments each user left:");
var lastCommentsPerUser = context.BlogPosts
.Select(post => post.Comments
.OrderByDescending(comment => comment.CreatedDate)
.FirstOrDefault())
.Where(comment => comment != null)
.GroupBy(comment => comment.UserName)
.Select(group => new {
UserName = group.Key,
LastCommentCount = group.Count()
})
.ToList();
foreach (var result in lastCommentsPerUser) {
Console.WriteLine($"{result.UserName}: {result.LastCommentCount}");
}
// Output blog post titles as JSON
Console.WriteLine(JsonSerializer.Serialize(context.BlogPosts.Select(x => x.Title).ToList()));
}
private static void InitializeData(MyDbContext context) {
context.BlogPosts.Add(new BlogPost("Post1") {
Comments = new List<BlogComment>()
{
new BlogComment("1", new DateTime(2020, 3, 2), "Petr"),
new BlogComment("2", new DateTime(2020, 3, 4), "Elena"),
new BlogComment("8", new DateTime(2020, 3, 5), "Ivan"),
}
});
context.BlogPosts.Add(new BlogPost("Post2") {
Comments = new List<BlogComment>()
{
new BlogComment("3", new DateTime(2020, 3, 5), "Elena"),
new BlogComment("4", new DateTime(2020, 3, 6), "Ivan"),
}
});
context.BlogPosts.Add(new BlogPost("Post3") {
Comments = new List<BlogComment>()
{
new BlogComment("5", new DateTime(2020, 2, 7), "Ivan"),
new BlogComment("6", new DateTime(2020, 2, 9), "Elena"),
new BlogComment("7", new DateTime(2020, 2, 10), "Ivan"),
new BlogComment("9", new DateTime(2020, 2, 14), "Petr"),
}
});
context.SaveChanges();
}
}
}