Na podstawie informacji z here.Jak zrobić ogólną funkcję za pomocą LINQ?
Znalazłem sposób usuwania sierot z Entity Framework.
public void SaveChanges()
{
context.ReportCards
.Local
.Where(r => r.Student == null)
.ToList()
.ForEach(r => context.ReportCards.Remove(r));
context.SaveChanges();
}
Zastanawiałem się, jak sprawić, by funkcję rodzajowy dla tej części, ponieważ może on być stosowany często:
context.ReportCards
.Local
.Where(r => r.Student == null)
.ToList()
.ForEach(r => context.ReportCards.Remove(r));
myślałem o czymś takim:
public void SaveChanges()
{
RemoveOrphans(Student, ReportCards)
context.SaveChanges();
}
private void RemoveOrphans<T>(T sourceContext, T orphan)
{
context.orphan
.Local
.Where(r => r.sourceContext == null)
.ToList()
.ForEach(r => context.orphan
.Remove(r));
}
Ale oczywiście nie działa. Jakakolwiek rada?
Dla 'Where' część po prostu przekazać w' Predicate' – TheLethalCoder
Prawdopodobnie chcesz użyć '' context.Set. –
juharr