Пример #1
0
        public static void MakeUniqueUpRows(UPRowsMergeHelperData data)
        {
            if (data.Rows.Count <= 0) return;

            var rowsToMergeGroup = data.AsEnumerable()
                .GroupBy(grp => grp[HelperDataColumnElementGUID.Name])
                .Where(yy => yy.Count() > 1)
                .ToList();

            if (rowsToMergeGroup.Any())
            {
                var rowsToMerge = rowsToMergeGroup.First();

                while (rowsToMerge != null)
                {

                    var rowsNrsToMerge = rowsToMerge
                        .Select(y => y.Field<string>(HelperDataStandartUPRowsUPFullRowNr.Name))
                        .ToList();

                    Log.InfoFormat("Объединяем ряды: {0}", string.Join("; ", rowsNrsToMerge));

                    // выясняем какие строки таблицы надо обновить
                    var rowsToUpdate = data.AsEnumerable()
                        .Where(x => x.Field<string>(HelperDataStandartUPRowsUPFullRowNr.Name) != rowsNrsToMerge.First() && rowsNrsToMerge.Contains(x.Field<string>(HelperDataStandartUPRowsUPFullRowNr.Name)))
                        .Select(x => x);

                    //апдейтим значения строк
                    foreach (var row in rowsToUpdate)
                        row[HelperDataStandartUPRowsUPFullRowNr.Name] = rowsNrsToMerge.First();

                    var uniqueRows = data.Copy().AsEnumerable()
                        .GroupBy(x => x.Field<string>(HelperDataColumnElementGUID.Name))
                        .Select(g => g.First());

                    foreach (var item in uniqueRows)
                    {
                        var existedRows = data.GetAllRowsByGUID(item.Field<string>(HelperDataColumnElementGUID.Name));
                        var rowToInsert = existedRows.FirstOrDefault();

                        if (existedRows.Count > 0 && rowToInsert != null)
                        {
                            var dataToInsert = rowToInsert.ItemArray;

                            foreach (var rowToDelete in existedRows)
                                data.Rows.Remove(rowToDelete);

                            data.Rows.Add(dataToInsert);
                        }
                    }

                    // обновляем расчетную переменную
                    rowsToMerge = data.AsEnumerable()
                        .GroupBy(grp => grp[HelperDataColumnElementGUID.Name])
                        .Where(yy => yy.Count() > 1)
                        .FirstOrDefault();
                }
                Log.DebugFormat("После объединения: {0}{1}", Environment.NewLine, data.Dump());

            }

            // перенумерация внутри ряда
            var orderedByOrderAndGroupedByRowNr = data.AsEnumerable().OrderBy(x => x.Field<int>(HelperDataUPRowsMergeUPOrder.Name)).GroupBy(row => row.Field<string>(HelperDataUPRowsUPMergeFullRowNr.Name));
            var currentNr = 1;
            foreach (var group in orderedByOrderAndGroupedByRowNr)
            {
                foreach (var element in @group.OrderBy(x => x.Field<int>(HelperDataUPRowsMergeUPOrder.Name)))
                {
                    element.SetField(HelperDataUPRowsUPMergeFullRowNr.Name, currentNr);
                }
                currentNr++;
            }
        }
        protected override BaseSentenceAlgorithmData InitSourceData(List<BaseSentenceAlgorithm> algorithms)
        {
            _upMergeHelperData = (UPRowsMergeHelperData)algorithms.FindHelperDataTableByAlgInternalName(AlgorithmNames.UniformParts.UPRowsMerge.InternalName);

            return algorithms.FindByInternalName(AlgorithmNames.DivorcedWords.ComplementUniformParts.InternalName).AlgResult;
        }