private dynamic GetNextIdValue(List <T> data) { if (!data.Any()) { return(ObjectExtensions.GetDefaultValue <T>(_idField)); } var lastItem = data.Last(); var expando = JsonConvert.DeserializeObject <ExpandoObject>(JsonConvert.SerializeObject(lastItem), new ExpandoObjectConverter()); // Problem here is if we have typed data with upper camel case properties but lower camel case in JSON, so need to use OrdinalIgnoreCase string comparer var expandoAsIgnoreCase = new Dictionary <string, dynamic>(expando, StringComparer.OrdinalIgnoreCase); if (!expandoAsIgnoreCase.ContainsKey(_idField)) { return(null); } dynamic keyValue = expandoAsIgnoreCase[_idField]; if (keyValue is Int64) { return((int)keyValue + 1); } return(ParseNextIntegertToKeyValue(keyValue.ToString())); }
private dynamic GetNextIdValue(List <T> data, T item = default(T)) { if (item != null) { dynamic primaryKeyValue = GetFieldValue(item, _idField); if (primaryKeyValue != null) { // Without casting dynamic will return Int64 for int if (primaryKeyValue is int ival) { if (data.Any(t => ((int)GetFieldValue(t, _idField)) == ival)) { return(data.Max(t => (int)GetFieldValue(t, _idField)) + 1); } } if (Guid.TryParse(primaryKeyValue, out Guid value)) { if (value != Guid.Empty && data.All(t => Guid.Parse(GetFieldValue(t, _idField)) != value)) { return(value); } else { return(Guid.NewGuid()); } } return(primaryKeyValue); } } if (!data.Any()) { return(ObjectExtensions.GetDefaultValue <T>(_idField)); } else { return(ParseNextIntegertToKeyValue(data.Max(t => GetFieldValue(t, _idField).ToString()))); } }
private dynamic GetNextIdValue(List <T> data, T item = default(T)) { if (!data.Any()) { if (item != null) { dynamic primaryKeyValue = GetFieldValue(item, _idField); if (primaryKeyValue != null) { // Without casting dynamic will return Int64 for int if (primaryKeyValue is Int64) { return((int)primaryKeyValue); } return(primaryKeyValue); } } return(ObjectExtensions.GetDefaultValue <T>(_idField)); } var lastItem = data.Last(); dynamic keyValue = GetFieldValue(lastItem, _idField); if (keyValue == null) { return(null); } if (keyValue is Int64) { return((int)keyValue + 1); } return(ParseNextIntegertToKeyValue(keyValue.ToString())); }