/// <summary> /// Get collection of entries where the second key is equal 'name' /// </summary> /// <param name="name"></param> /// <returns></returns> public IEnumerable <KeyValuePair <KeyStruct, TValue> > this[TName name] { get { if (name == null) { throw new ArgumentNullException(nameof(name)); } try { var isSuccess = _dNameId.TryGetValue(name, out var idList); if (!isSuccess || idList.Count == 0) { throw new KeyNotExistsException($"Requested Key {name} - does not exist"); } IEnumerable <KeyValuePair <KeyStruct, TValue> > resultEnumerable; if (idList.Count == 1) { var strKey = new KeyStruct(idList[0], name); resultEnumerable = _dictionary.Where(x => x.Key.Equals(strKey)); } else // IdList contains more than 1 entries for uniq name. "A" - 1; "A" - 2; "A" - 3 { var keyStructs = new KeyStruct[idList.Count]; for (int i = 0; i < idList.Count; i++) { keyStructs[i] = new KeyStruct(idList[i], name); } resultEnumerable = keyStructs.Select(x => new KeyValuePair <KeyStruct, TValue>(x, _dictionary[x])); } return(resultEnumerable); } catch (Exception e) { throw new KeyNotExistsException($"Requested Key {name} - does not exist", e); } } }
/// <summary> /// Get collection of entries where the first key is equal 'id' /// </summary> /// <param name="id"></param> /// <returns></returns> public IEnumerable <KeyValuePair <KeyStruct, TValue> > this[TId id] { get { if (id == null) { throw new ArgumentNullException(nameof(id)); } try { var isSuccess = _dIdName.TryGetValue(id, out var namesList); if (!isSuccess || namesList.Count == 0) { throw new KeyNotExistsException($"Requested Key {id} - does not exist"); } IEnumerable <KeyValuePair <KeyStruct, TValue> > resultEnumerable; if (namesList.Count == 1) { var strKey = new KeyStruct(id, namesList[0]); resultEnumerable = _dictionary.Where(x => x.Key.Equals(strKey)); } else // NamesList contains more than 1 entries for uniq id. 1-"A" 1-"B" 1-"C" { var keyStructs = new KeyStruct[namesList.Count]; for (int i = 0; i < namesList.Count; i++) { keyStructs[i] = new KeyStruct(id, namesList[i]); } resultEnumerable = keyStructs.Select(x => new KeyValuePair <KeyStruct, TValue>(x, _dictionary[x])); } return(resultEnumerable); } catch (Exception e) { throw new KeyNotExistsException($"Requested Key {id} - does not exist", e); } } }