Fixed Angular Material and Karma Error

If you are having trouble with Unknown ‘Angular Material Module’ element in your Angular Unit Test.
This could be the fix.

Add the Angular Material Module to the imports in beforeEach.

import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MatSidenavModule, MatCardModule } from '@angular/material';
import { DayscheduleComponent } from './../dayschedule/dayschedule.component';
import { WeekscheduleComponent  } from './../weekschedule/weekschedule.component';

import * as moment from 'moment';



describe('SchedulesComponent', () => {
  let component: SchedulesComponent;
  let fixture: ComponentFixture<SchedulesComponent>;

  let debugElement: DebugElement;
  let htmlElement: HTMLElement;

  beforeEach(
    async(() => {
      TestBed.configureTestingModule({
        declarations: [SchedulesComponent, DayscheduleComponent, WeekscheduleComponent],
        imports: [ MatSidenavModule, MatCardModule, HttpModule, BrowserModule, BrowserAnimationsModule ]
      }).compileComponents();
    })
  );

C#Fluent Assertion with InMemoryDatabase UnitTesting

 // assert each employee has morning and afternoon shifts
 var employees = context.Employees;
 foreach (var emp in employees)
 {
     var empShifts = context.EmployeeShifts.Where(x => x.EmployeeId == emp.Id).ToList();
     empShifts.Count().Should().Be(10);
     empShifts.Where(x => x.ShiftSchedule == ENUMERATIONS.ShiftSchedule.firstHalf).Count().Should().Be(5);
     empShifts.Where(x => x.ShiftSchedule == ENUMERATIONS.ShiftSchedule.secondHalf).Count().Should().Be(5);
  }

  // assert if no consecutive days shift of each employee
  foreach (var emp in employees)
  {
      var empShifts = context.EmployeeShifts.Where(x => x.EmployeeId == emp.Id).ToList();
                 
       foreach (var shift in empShifts)
       {
            var shiftDate = shift.Date;

            var nextDate = shift.Date.AddDays(1);
            var prevDate = shift.Date.AddDays(-1);

            var nextShift = empShifts.Where(x => x.Date == nextDate).FirstOrDefault();
            var prevShift = empShifts.Where(x => x.Date == prevDate).FirstOrDefault();
                       
            nextShift.Should().BeNull();
            prevShift.Should().BeNull();
                       
       }


  }

Delegates and Functional C# example again using Extension Methods

public class Planet
{
    public string Name { get; set; }
}

public static Planet AddXtoName(this Planet o)
{
    o.Name = o.Name + "X";
    return o;
}

public static Planet AddYToName(this Planet o)
{
    o.Name = o.Name + "Y";
    return o;
}

public static void Execute(this Planet o, Action cmd)
{
    cmd();
}

public static void Execute2(this Planet o, Action<string> cmd)
{
    cmd(o.Name);
}

public static int Execute3(this Planet o, Func<string, int> cmd)
{
    return cmd(o.Name);
}

var px = new Planet();
px.Name = "Jupiter";
px.AddXtoName().AddYToName();

px.AddYToName().AddXtoName();
px.Execute(() =>
{
    Console.WriteLine("Execute this!");
});

px.Execute2((x) =>
{
    Console.WriteLine($"Name of Planet is: {x}");
});

px.Execute2((x) =>
{
    Console.WriteLine($"This is also another execution of Exe2 for Name of: {x}");
});

var len = px.Execute3((x) =>
{
    return x.Length;
});

var len2 = px.Execute3((x) =>
{
    return x.Length + 100;
});

Console.WriteLine(len);
Console.WriteLine(len2);
Console.WriteLine("");

Example of Functional C# using extension Methods

public class Calculor {
    public int Total { get; set; }
}
public class Calculator {
    public int Total { get; set; }
}

public static Calculator Add(this Calculator c, int number) {
    c.Total += number;
    return c;
}
public static Calculator Minus(this Calculator c, int number) {
    c.Total -= number;
    return c;
}
public static Calculator Times(this Calculator c, int number) {
    c.Total *= number;
    return c;
}
public static Calculator Divide(this Calculator c, int number) {
    c.Total = c.Total / number;
    return c;
}

var c = new Calculator();
c.Add(100).Add(50);

Console.WriteLine(c.Total);
150
c.Minus(30);
Console.WriteLine(c.Total);
120
public static object Addx<T>(this T c, int number) {
    object obj = (object)c;

    var p = obj.GetType().GetProperty("Total");
    object itemValue = p.GetValue(obj, null);
    int total = (int)itemValue;
    total += number;
    p.SetValue(obj, total, null);
    return obj;
}

var cc = new Calculator();
cc.Addx<Calculator>(888);
Console.WriteLine(cc.Total);

Console.WriteLine("");
888

public class CalculatorX{
    public int Total { get; set; }
}
var cx = new CalculatorX();
cx.Addx<CalculatorX>(777);
Console.WriteLine(cx.Total);
Console.WriteLine("");

Add-Migration Entity Framework Core (ef core) Nothing Happens

If you are out there wondering what is going on to your

add-migration

or

update-database

, same thing happened to me.
I have multiple startup projects in my Visual Studio 2017. The fix is to use your main WebApi / WebApp Project as the start up project
first before running the

add-migration

again and walahhh you are good to go kid. Your welcome.

Func Type and Extension Methods in C#

Here is a sample code Func is useful and Extension Methods.

using System;

class MainClass {
  public static void Main (string[] args) {
   
    var Gouko = new Hero();
    Gouko.Name = "Gouko";
    Gouko.Energy = 100;
    Gouko.LevelUp = x => x.RashingGun().RayGun().KamihamiWave();
    var powerLevel = Gouko.LevelUp(10);
    Console.WriteLine(powerLevel);
   
    var Naruto = new Hero();
    Naruto.Name = "Naruto";
    Naruto.Energy = 50;
    Naruto.LevelUp = x => {
      var z = 5;
      return z * x;
    };
    var naruloLevel = Naruto.LevelUp(5);
    Console.WriteLine(naruloLevel);
  }
 
}

public interface IHero{
}

public class Hero : IHero
{
  public string Name { get; set; }
  public int Energy{ get; set; }
  public Func<int,int> LevelUp { get; set; }
 
}
 
public static class HeroPipe{
  public static int RashingGun(this int power){
    return power * 2;
  }
  public static int RayGun(this int power){
    return power * 10;
  }
  public static int KamihamiWave(this int power){
    return power * 100;
  }
}

Play with the code: Repl.it

C# Linq Grouping and Distinct

var px = parents.SelectMany(p => p.Childs.Select(c => new {
                        number = c.Number,
                        parent = p
                 })).GroupBy(g => g.number).Distinct().Select(s => s.First())
                .GroupBy(g => g.parent)
                .Select(s => s.First());

Linq Sample to Join 3 Collections / Tables

void Main()
{
    var members = Members();
    var locations = Locations();
    var planets = Planets();
    var l = members
                .Join(locations,
                      mem => mem.mId,
                      loc => loc.mId,                      
                      (mem, loc) => new { loc, mem })
                 .Join(planets,
                       lm => lm.loc.mId,
                       pla => pla.mId,
                       (lm,pl) => new { pl.Name, lm.loc, lm.mem })   
                  .Select(s => new {
                     s.mem.FullName,
                     s.Name,
                     s.loc.Address                   
                  })
                  .ToList();                   
     l.Dump();
}

public List<Planet> Planets(){
  var x = new List<Planet>(){
     new Planet(){ mId = 1, Name = "Mercury" },
     new Planet(){ mId = 2, Name = "Venus" },
     new Planet(){ mId = 3, Name = "Earth" },
     new Planet(){ mId = 4, Name = "Mars" }
  };
  return x;
}

public List<Location> Locations(){
   var x = new List<Location>(){
      new Location(){ mId = 1, Address = "Busugon" },
      new Location(){ mId = 2, Address = "Tacup" },
      new Location(){ mId = 3, Address = "Kinawahan" },
      new Location(){ mId = 4, Address = "Victoria" },
   };
   return x;
}

public List<Member> Members(){
    var x = new List<Member>(){
       new Member(){ mId = 1, FullName = "Julius", Number = 1 },
       new Member(){ mId = 2, FullName = "Elvira", Number = 2 },
       new Member(){ mId = 3, FullName = "Roberto",  Number = 3 }      
    };
    return x;
}

public class Member {
    public int mId { get; set; }
    public string FullName { get; set; }
    public int Number { get; set; }
}

public class Location {
   public int mId { get; set; }
   public string Address { get;set; }
}

public class Planet {
   public int mId { get; set; }
   public string Name { get; set; }
}

Printing Directly from Asp.Net MVC Controller Action to Printer in the Network

public async Task<ActionResult> Generate(string id)
{
     PrintDocument pd = new PrintDocument();
     pd.DefaultPageSettings.Margins.Left = 30;
     pd.DefaultPageSettings.Margins.Top = 10;
     pd.DefaultPageSettings.Margins.Right = 10;
     pd.DefaultPageSettings.Margins.Bottom = 10;
     pd.OriginAtMargins = true;
     pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
     // Set the printer name.
     //pd.PrinterSettings.PrinterName = "\\NS5\hpoffice
     //pd.PrinterSettings.PrinterName = "HP LaserJet 200 color M251 PCL6 Class Driver";
     pd.PrinterSettings.PrinterName = "EPSON LX-310";
     pd.Print();

     return RedirectToAction("Index", "Payslip", new { area = "Accounting", id = "", code = 801 });
}

void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
     Font printFont = new Font("3 of 9 Barcode", 18);
     Font printFont1 = new Font("Times New Roman", 11, FontStyle.Bold);
     SolidBrush br = new SolidBrush(Color.Black);
     ev.Graphics.DrawString("hello", printFont, br, 10, 65);
     ev.Graphics.DrawString("world", printFont1, br, 10, 85);
}

C# Base Repository Interface

For those Developers using Repository Pattern, this is the ideal structure of your Base Repository Interface.

public interface IBaseRepository{
  Task<IEnumerable<Entity>> FindAll();
  Task<IEnumerable<Entity>> FindAll(Expression<Func<Entity, bool>> predicate);
  Task<Entity> GetByID(int id);
  void Delete(int id);
  Task<Entity> SaveOrUpdate(Entityentity);
  Task<Entity> SaveOrUpdate(int id, Entityentity);
  void BulkSaveOrUpdate(List<Entity> entities);
}