private ActionDTO SimplifyOperations(IGrouping <int, ActionDTO> g) { ActionDTO result; if (g.Any(a => a.ActionType == ActionType.Delete && g.Key > 0)) { result = new ActionDTO { Id = g.Key, ActionType = ActionType.Delete } } ; else if (g.Any(a => a.ActionType == ActionType.Add && g.Key < 0)) { var last = g.LastOrDefault(a => a.ActionType == ActionType.Modify); result = new ActionDTO { Id = g.Key, ActionType = ActionType.Add, Text = last?.Text, Date = last?.Date }; } else if (g.Key > 0) { var last = g.Last(a => a.ActionType == ActionType.Modify); result = new ActionDTO { Id = g.Key, ActionType = ActionType.Modify, Text = last.Text, Date = last.Date }; } else { throw new HttpResponseException(HttpStatusCode.BadRequest); } return(result); }
private static Feature CollectNewColumnValues(IGrouping <string, Feature> groupFeature, Dictionary <string, string> newColumnValues, OperatorPair operatorPair) { Feature feature = new Feature(); switch (operatorPair.Operator) { case DissolveOperatorMode.First: feature = groupFeature.FirstOrDefault(); if (feature.GetShape() != null) { newColumnValues.Add(operatorPair.ColumnName, feature.ColumnValues[operatorPair.ColumnName]); } break; case DissolveOperatorMode.Last: feature = groupFeature.LastOrDefault(); if (feature.GetShape() != null) { newColumnValues.Add(operatorPair.ColumnName, feature.ColumnValues[operatorPair.ColumnName]); } break; case DissolveOperatorMode.Count: newColumnValues.Add(operatorPair.ColumnName, groupFeature.Count().ToString()); break; case DissolveOperatorMode.Sum: DissolveNumbers(groupFeature , newColumnValues , operatorPair , rs => rs.Sum() , rs => rs.Sum()); break; case DissolveOperatorMode.Average: DissolveNumbers(groupFeature , newColumnValues , operatorPair , rs => rs.Average()); break; case DissolveOperatorMode.Min: DissolveNumbers(groupFeature , newColumnValues , operatorPair , rs => rs.Min() , rs => rs.Min()); break; case DissolveOperatorMode.Max: DissolveNumbers(groupFeature , newColumnValues , operatorPair , rs => rs.Max() , rs => rs.Max()); break; } return(feature); }
private bool GetConditionForAnswersWrongToWrongFirsAndtLast(IGrouping <string, Snapshot> snapsForEachItem, string correctAnswer, string lastAnswer) { var isLastAnswerWrong = lastAnswer != correctAnswer; var snapshotOfFirstAnswer = snapsForEachItem.FirstOrDefault(z => !string.IsNullOrEmpty(z.Response)); var snapshotOfLastAnswer = snapsForEachItem.LastOrDefault(); if (snapshotOfFirstAnswer != null && isLastAnswerWrong) { if (lastAnswer != snapshotOfFirstAnswer.Response) { return(correctAnswer != snapshotOfFirstAnswer.Response); } } return(false); }
// Tuple // item1: columnName, // item2: columnType, // item3: OperatorMode. private static Feature CollectNewColumnValues(IGrouping <string, Feature> groupFeature, Dictionary <string, string> newColumnValues, Tuple <string, string, DissolveOperatorMode> operatorPair) { PairStrategy pairStrategy = new PairStrategy { ColumnName = operatorPair.Item1, ColumnType = operatorPair.Item2, Operator = operatorPair.Item3 }; var getIntColumnValueFunc = new Func <Feature, int>(f => { int currentValue = 0; if (f.ColumnValues.ContainsKey(pairStrategy.ColumnName)) { Int32.TryParse(f.ColumnValues[pairStrategy.ColumnName], out currentValue); } return(currentValue); }); var getDoubleColumnValueFunc = new Func <Feature, double>(f => { double currentValue = 0; if (f.ColumnValues.ContainsKey(pairStrategy.ColumnName)) { Double.TryParse(f.ColumnValues[pairStrategy.ColumnName], out currentValue); } return(currentValue); }); Feature feature = new Feature(); switch (pairStrategy.Operator) { case DissolveOperatorMode.First: feature = groupFeature.FirstOrDefault(); if (feature.GetShape() != null) { newColumnValues.Add(pairStrategy.ColumnName, feature.ColumnValues[pairStrategy.ColumnName]); } break; case DissolveOperatorMode.Last: feature = groupFeature.LastOrDefault(); if (feature.GetShape() != null) { newColumnValues.Add(pairStrategy.ColumnName, feature.ColumnValues[pairStrategy.ColumnName]); } break; case DissolveOperatorMode.Count: newColumnValues.Add(pairStrategy.ColumnName, groupFeature.Count().ToString()); break; case DissolveOperatorMode.Sum: if (pairStrategy.ColumnType.Equals("Integer", StringComparison.OrdinalIgnoreCase)) { int intSum = groupFeature.Sum(getIntColumnValueFunc); newColumnValues.Add(pairStrategy.ColumnName, intSum.ToString()); } else if (pairStrategy.ColumnType.Equals("Double", StringComparison.OrdinalIgnoreCase)) { double doubleSum = groupFeature.Sum(getDoubleColumnValueFunc); newColumnValues.Add(pairStrategy.ColumnName, doubleSum.ToString()); } break; case DissolveOperatorMode.Average: if (pairStrategy.ColumnType.Equals("Integer", StringComparison.OrdinalIgnoreCase) || pairStrategy.ColumnType.Equals("Double", StringComparison.OrdinalIgnoreCase)) { double averageValue = groupFeature.Average(getDoubleColumnValueFunc); newColumnValues.Add(pairStrategy.ColumnName, averageValue.ToString()); } break; case DissolveOperatorMode.Min: if (pairStrategy.ColumnType.Equals("Integer", StringComparison.OrdinalIgnoreCase)) { int intMin = groupFeature.Min(getIntColumnValueFunc); newColumnValues.Add(pairStrategy.ColumnName, intMin.ToString()); } else if (pairStrategy.ColumnType.Equals("Double", StringComparison.OrdinalIgnoreCase)) { double doubleMin = groupFeature.Sum(getDoubleColumnValueFunc); newColumnValues.Add(pairStrategy.ColumnName, doubleMin.ToString()); } break; } return(feature); }