private T Single(string field, BsonValue value, bool errorIfMissing) { using (var db = _db.OpenRead()) { var coll = GetCollection(db); if (coll.Count() == 0) { return(DefaultOrThrow(field, value, errorIfMissing)); } var matches = coll.Find(Query.EQ(field, value)); if (!matches.Any()) { return(DefaultOrThrow(field, value, errorIfMissing)); } if (matches.Count() > 1) { throw DuplicateRecordsException.For(matches, field, value); } return(matches.Single()); } }
internal static void RejectDuplicateRecord <T>(this ISimpleRepo <T> repo, Func <T, bool> predicate, string field, T record, Func <T, int> idGetter = null) //where T : IDocumentDTO { var matches = repo.GetAll().Where(predicate); if (!matches.Any()) { return; } if (matches.Count() > 1) { throw DuplicateRecordsException .For(matches, field, record.ToString()); } if (matches.Count() == 1 && idGetter != null) { var recId = idGetter(record); var matchId = idGetter(matches.Single()); if (recId == matchId) { return; } } throw DuplicateRecordsException .For(matches, field, record.ToString()); }
private FundRequestDTO FindInactiveRequest(ChequeVoucherDTO chq) { var matches = InactiveRequests.Find(_ => _.SerialNum == chq.Request.SerialNum); if (!matches.Any()) { //throw No.Match<FundRequestDTO>("SerialNum", chq.Request.SerialNum); InactiveRequests.Insert(chq.Request); return(FindInactiveRequest(chq)); } if (matches.Count() > 1) { throw DuplicateRecordsException.For(matches, "SerialNum", chq.Request.SerialNum); } return(matches.Single()); }
public CollectorDTO CollectorByName(string collectorName, bool errorIfNoMatch = true) { var matches = _colctrsById.Values.Where(_ => _.Name == collectorName); if (matches.Count() == 1) { return(matches.First()); } if (matches.Count() > 1) { DuplicateRecordsException.For(matches, "Name", collectorName); } if (!errorIfNoMatch) { return(null); } throw No.Match <CollectorDTO>("Name", collectorName); }