public virtual void AddMultipleStockEntries(IEnumerable <StockEntry> stockentries) { //TODO:Add Validation var existingAndNewEntries = stockentries.Select(st => (Existing: GetByNfCode(st.NfNumber), Given: st)); var newEntries = existingAndNewEntries.Where(st => st.Existing is null) .Select(st => st.Given); var existingEntries = existingAndNewEntries.Where(st => !(st.Existing is null)); foreach (var(Existing, Given) in existingEntries) { Given.Items.ToList().ForEach(given => Existing.AddEntry(given)); } _stockEntryRepository.AddRange(newEntries); _stockEntryRepository.AddRange(existingEntries.Select(st => st.Existing)); _stockEntryRepository.SaveChanges(); }