protected void CreateTableButton_Click(object sender, EventArgs e) { int datefieldId = 0; Int32.TryParse(DateForFilter.SelectedValue, out datefieldId); int fieldId = 0; Int32.TryParse(FiedsDropDownList.SelectedValue, out fieldId); int fieldToSerachIn = 0; Int32.TryParse(FilterFiedsDropDownList.SelectedValue, out fieldToSerachIn); DateTime startDate = DateTime.MinValue; DateTime.TryParse(StartDateTextBox.Text, out startDate); DateTime endDate = DateTime.MaxValue; DateTime.TryParse(EndDateTextBox.Text, out endDate); if (endDate.Year < 10) { endDate = DateTime.MaxValue; } DateTime LastChangeStartDate = DateTime.MinValue; DateTime.TryParse(LastChangedDateStartTextBox.Text, out LastChangeStartDate); DateTime LastChangeEndDate = DateTime.MaxValue; DateTime.TryParse(LastChangedDateEndTextBox.Text, out LastChangeEndDate); if (LastChangeEndDate.Year < 10) { LastChangeEndDate = DateTime.MaxValue; } string valueToSearch = ""; valueToSearch = SearchInFieldTextBox.Text; int registerId = 0; Int32.TryParse(RegistersDropoDownList.SelectedValue, out registerId); //Получили все параметры со страницы StatisticsClass stClass = new StatisticsClass(); List <CollectedFieldsValues> resultList = stClass.GetAllCollectedForFieldInDateRange(registerId, fieldId, datefieldId, startDate, endDate, fieldToSerachIn, valueToSearch); resultList = (from a in resultList where a.CreateDateTime >= LastChangeStartDate && a.CreateDateTime <= LastChangeEndDate select a).Distinct().ToList(); List <int> cardIds = (from a in resultList select a.FkCollectedCard).Distinct().ToList(); Session["cardsIds"] = cardIds; List <string> uniqueValues = (from a in resultList orderby a.ValueText.Trim() select a.ValueText.Trim()).Distinct().ToList(); Table resultTable = new Table() { CssClass = "resultTable" }; resultTable.Rows.Add(new TableRow() { Cells = { new TableCell() { Text = "Название" }, new TableCell() { Text = "Кол-во" }, new TableCell() { Text = "Список номеров" } } }); int sum = 0; foreach (string current in uniqueValues) { List <CollectedFieldsValues> res = (from a in resultList where a.ValueText.Trim() == current select a).ToList(); //List<string> = res.Select(mc=>mc.) List <CollectedCards> cards = new List <CollectedCards>(); foreach (CollectedFieldsValues tmp in res) { cards.Add(main.GetCardById(tmp.FkCollectedCard)); } string cardIdsText = String.Join(", ", cards.Select(mc => mc.MaInFieldID).ToList()); int count = res.Count(); sum += count; resultTable.Rows.Add(new TableRow() { Cells = { new TableCell() { Text = current }, new TableCell() { Text = count.ToString() }, new TableCell() { Text = cardIdsText } } }); } resultTable.Rows.Add(new TableRow() { Cells = { new TableCell() { Text = "Итого" }, new TableCell() { Text = sum.ToString() } } }); resultDiv.Controls.Add(resultTable); }