Comrades, there is a DataTable in it there is an Article column.

How to write to the array (or where it would be better) the number of records encountered.

For example, there are 5 entries

  1. 228
  2. 105
  3. 228
  4. 100
  5. 105

I need him to record what

228 - 2 times

105 - 2

100 - 1

How much better to write?

I know how to write to a variable for a specific criterion, but it is necessary that he write out what has happened and how much.

FaceOutCount = dt.AsEnumerable().Where(x => x["ФИО подозреваемого"].ToString() == "н/л").ToList().Count; 

I will then need to rewrite it in Word (but this is me)

I hope lucidly wrote, in words it sounds easier))

Help)

  • If the elements are unique, then it is possible in the dictionary, where the keys are your elements, and the values ​​are the frequency of occurrence. Scroll through all the lines in the loop. If it is still not clear, I can explain with an example. - Setplus
  • @Setplus Suck example. Elements of this value in the cell? (228 or 105) If yes, then the dictionary is not an option. Their huge amount. 228 or 105 are articles of the Criminal Code. there are 360 ​​+ varieties with parts and points. - ne0n
  • @Setplus We need to run over the column ourselves, he met 228, wrote down 228 - 1, if he meets, he adds 228 - 2 or as many times as he meets - ne0n
  • why not an option? There will be 380 pairs in total, the values ​​will be ulong or uint. And all the rules will be. - Setplus
  • @Setplus Well, please type in an example, I don’t understand what works, what you offer) - ne0n

3 answers 3

If I correctly captured the essence of the task, then without further ado, here's the code

  public static DataTable GetTable() { // Here we create a DataTable with four columns. DataTable table = new DataTable(); table.Columns.Add("id", typeof(int)); table.Columns.Add("numOfArticle", typeof(int)); table.Rows.Add(1, 228); table.Rows.Add(2, 105); table.Rows.Add(3, 228); table.Rows.Add(4, 100); table.Rows.Add(5, 105); return table; } static void Main(string[] args) { DataTable dt = GetTable(); Dictionary<int, int> dictStats = new Dictionary<int, int>(); foreach (DataRow row in dt.Rows) { if (!dictStats.ContainsKey(row.Field<int>(1))) dictStats.Add(row.Field<int>(1), 1); else dictStats[row.Field<int>(1)]++; } Console.WriteLine("Press any key..."); Console.ReadKey(); } 

Here is a cycle in rows, but, obviously, it will be similar if it is organized in columns.

     //Для теста. var list = new List<int> {228, 105, 228, 100, 105}; //Основной код (группируем итемы по определенному значению, //создаем новое отображение, где в анонимные типы вносим кол-во элементов в группе //и сам элемент). var count = list.GroupBy(g => g) .Select(s => new {Id = s.Key, Count = s.Count()}); //Выводим результат (пример того, как обращаться). foreach (var item in count) Console.WriteLine($"Число: {item.Id} | Кол-во: {item.Count}"); 

    Conclusion:

     Число: 228 | Кол-во: 2 Число: 105 | Кол-во: 2 Число: 100 | Кол-во: 1 
        //Колво статей int[] countArticle = new int[dt.Rows.Count]; string[] nameArticle = new string[dt.Rows.Count]; for (int i = 0;i < dt.Rows.Count; i++) { nameArticle[i] = dgvArchive.Rows[i].Cells[2].Value.ToString(); countArticle[i] = 0; for(int j = 0; j < dt.Rows.Count; j++) { if (nameArticle[i] == dgvArchive.Rows[j].Cells[2].Value.ToString()) { countArticle[i]++; } } } //Удаляем одинаковые записи string n = ""; for (int i = 0; i < nameArticle.Length; i++) { n = nameArticle[i]; for (int j = 0; j < nameArticle.Length; j++) { if (n == nameArticle[j] && j != i) { nameArticle = DeleteElementFromMassString(nameArticle.ToList(), j); countArticle = DeleteElementFromMassInt(countArticle.ToList(), j); } } } public string[] DeleteElementFromMassString(List<string> array, int indexToDelete) { array.RemoveAt(indexToDelete); return array.ToArray(); } public int[] DeleteElementFromMassInt(List<int> array, int indexToDelete) { array.RemoveAt(indexToDelete); return array.ToArray(); } 
      • Thank you user: 297036 - ne0n