PI06 i PI06-1. Docker definitions for MSSQL and Postgres. Data seeder/generator for countries and people. Entity Framework example with variants for Postgres and MSSQL
This commit is contained in:
16
SomeOfCSharpFeatures/Extensions/Activity.cs
Normal file
16
SomeOfCSharpFeatures/Extensions/Activity.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Extensions;
|
||||
|
||||
[Flags]
|
||||
public enum DayPeriod
|
||||
{
|
||||
Morning = 1, Evening = 2, Afternoon = 4, Night = 8
|
||||
}
|
||||
|
||||
public class Activity
|
||||
{
|
||||
public required string Person { get; set; }
|
||||
public int Hours { get; set; }
|
||||
public DayPeriod Period { get; set; }
|
||||
}
|
||||
18
SomeOfCSharpFeatures/Extensions/Extensions.cs
Normal file
18
SomeOfCSharpFeatures/Extensions/Extensions.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Extensions;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static V GetOrCreate<K, V>(this Dictionary<K, V> dict, K key)
|
||||
where V : new()
|
||||
where K: notnull
|
||||
{
|
||||
if (!dict.TryGetValue(key, out V? value))
|
||||
{
|
||||
value = new V();
|
||||
dict[key] = value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
10
SomeOfCSharpFeatures/Extensions/Extensions.csproj
Normal file
10
SomeOfCSharpFeatures/Extensions/Extensions.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
61
SomeOfCSharpFeatures/Extensions/Program.cs
Normal file
61
SomeOfCSharpFeatures/Extensions/Program.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
namespace Extensions;
|
||||
|
||||
public enum Days
|
||||
{
|
||||
Monday, Tuesday, Wednesday,
|
||||
Thursday, Friday, Saturday, Sunday
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var dict = new Dictionary<Days, List<Activity>>();
|
||||
Activity a = new Activity
|
||||
{
|
||||
Person = "John",
|
||||
Hours = 3,
|
||||
Period = DayPeriod.Morning
|
||||
};
|
||||
dict.GetOrCreate(Days.Monday).Add(a);
|
||||
|
||||
a = new Activity
|
||||
{
|
||||
Person = "Mary",
|
||||
Hours = 12,
|
||||
Period = DayPeriod.Evening | DayPeriod.Night
|
||||
};
|
||||
dict.GetOrCreate(Days.Monday).Add(a);
|
||||
|
||||
a = new Activity
|
||||
{
|
||||
Person = "Peter",
|
||||
Hours = 8,
|
||||
Period = DayPeriod.Night
|
||||
};
|
||||
dict.GetOrCreate(Days.Tuesday).Add(a);
|
||||
|
||||
a = new Activity
|
||||
{
|
||||
Person = "Lisa",
|
||||
Hours = 5,
|
||||
Period = DayPeriod.Night | DayPeriod.Morning
|
||||
};
|
||||
dict.GetOrCreate(Days.Monday).Add(a);
|
||||
|
||||
int sum = SumNightActivity(dict);
|
||||
Console.WriteLine(sum);
|
||||
|
||||
}
|
||||
|
||||
private static int SumNightActivity(Dictionary<Days, List<Activity>> dict)
|
||||
{
|
||||
int sum = 0;
|
||||
foreach(var list in dict.Values)
|
||||
{
|
||||
sum += list.Where(a => (a.Period & DayPeriod.Night) == DayPeriod.Night)
|
||||
.Sum(a => a.Hours);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user