.NET 4.x Найти повторяющееся значение и вывести имя свойства - C#

Узнай цену своей работы

Формулировка задачи:

Имеется
Листинг программы
  1. public class Part
  2. {
  3. public string Designation {get; set;}
  4. public string Name {get; set;}
  5. public int Count {get; set;}
  6. public Part (string _name, string _designation, int _count)
  7. {
  8. Name = _name;
  9. Designation = _designation;
  10. Count = _count;
  11. }
  12. }
  13. //......................
  14. public ObservableCollection<Part> PartCollection { get; set; }
  15. PartCollection.Add(new Part("Деталька","АБВГ", 3);
  16. PartCollection.Add(new Part("Деталька","АБВ", 1);
  17. PartCollection.Add(new Part("Деталька","АБВГ",43);
  18. //......................
Вопрос: как узнать по каким свойствам совпадают классы в коллекции, вывести имя свойства совпадающих и значение свойства. Т.е. найти дубляж по двум свойствам и вывести этот дубляж...да хоть в консоль)))

Решение задачи: «.NET 4.x Найти повторяющееся значение и вывести имя свойства»

textual
Листинг программы
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Reflection;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     namespace ConsoleApplication1
  8.     {
  9.         class Program
  10.         {
  11.             static void Main(string[] args)
  12.             {
  13.                 ObservableCollection<Part> PartCollection = new ObservableCollection<Part>();
  14.                 PartCollection.Add(new Part("Деталька", "АБВГ", 3));
  15.                 PartCollection.Add(new Part("Деталька", "АБВ", 1));
  16.                 PartCollection.Add(new Part("Деталька", "АБВГ", 43));
  17.  
  18.                 string result = "";
  19.  
  20.                 for (int i = 0; i < PartCollection.Count; i++)
  21.                     for (int j = i + 1; j < PartCollection.Count; j++)
  22.                     {
  23.                         var part1 = PartCollection[i];
  24.                         var part2 = PartCollection[j];
  25.                         string matches = Matches(part1, part2);
  26.                         if (!string.IsNullOrEmpty(matches))
  27.                             result += $"Для Part {part1.Name} и Part {part2.Name} найдены следующие совпадения:\n{matches}\n";
  28.                     }
  29.                 Console.WriteLine(result);
  30.             }
  31.  
  32.             public class Part
  33.             {
  34.                 public string Designation { get; set; }
  35.                 public string Name { get; set; }
  36.                 public int Count { get; set; }
  37.                 public Part(string _name, string _designation, int _count)
  38.                 {
  39.                     Name = _name;
  40.                     Designation = _designation;
  41.                     Count = _count;
  42.                 }
  43.             }
  44.             private static string Matches(Part part1, Part part2)
  45.             {
  46.                 string result = "";
  47.                 int matches = 0;
  48.  
  49.                 var pi = typeof(Part).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty);
  50.  
  51.                 foreach (var p in pi)
  52.                     if (p.GetValue(part1).Equals(p.GetValue(part2)))
  53.                     {
  54.                         matches++;
  55.                         result += $"Имя свойства: {p.Name}; Значение свойства: {p.GetValue(part1)}\n";
  56.                     };
  57.  
  58.                 if (matches >= 2) return result;
  59.                 else return "";
  60.             }
  61.         }
  62.     }
  63. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 3.583 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы