Cześć mam dwie klasy :)
Pracownik:
public class Pracownik
{
public long Id { get; private set; }
public string Kod { get; private set; }
public string Imię { get; private set; }
public string Nazwisko { get; private set; }
public string ImięNazwisko { get; private set; }
public ICollection<PracHistoria> Historia { get; private set; }
public PracHistoria Last => Historia.LastOrDefault();
protected Pracownik() { }
public Pracownik(string imie, string nazwisko)
{
Imię = imie;
Nazwisko = nazwisko;
ImięNazwisko = imie + " " + nazwisko;
Kod = String.Format("{0:D5}", Id);
Historia = new List<PracHistoria>();
}
}
PracHistoria
public class PracHistoria
{
public long Id { get; private set; }
public DateTime Aktualnosc { get; private set; }
public long PracownikId { get; private set; }
public Pracownik Pracownik { get; private set; }
protected PracHistoria() { }
public PracHistoria(DateTime aktualnosc, long pracownikId)
{
Aktualnosc = aktualnosc;
PracownikId = pracownikId;
}
}
jak napisać indekser aby w klasie Program mógłbym użyć takiej składni :)
ApplicationDbContext dbContext = new ApplicationDbContext();
var p = dbContext.Pracownicy.Include(x=>x.Historia).FirstOrDefault();
Console.WriteLine(p.Historia[DateTime.Now]);
próbowałem w klasie PracHistoria napisać coś takiego ale nie pomaga :/
public PracHistoria this[DateTime now]
{
get
{
if (Aktualnosc == now)
{
return this;
}
else return null;
}
}