Checking for duplication of the body elements



  • Gentlemen, help with this task. We need to check the element of MyReaderData.TagList[i].ToString() to duplicate :

    private void HandleData(Symbol.RFID.ReaderData TheReaderData)
    {
        for (int i = 0; i < MyReaderData.TagList.TotalTags; i++)
        {
            string[] sItems = new string[] 
                {
                    i.ToString(), 
                    MyReaderData.TagList[i].ToString() 
                };
            this.ReaderDataListView.Items.Add(new ListViewItem(sItems));
         }
    }       
    


  • In order to meet the challenge, the means must be used. Linq

    //Создаем список объектов
    var rows1 = Enumerable.Range(1,50).Select(x=> Guid.NewGuid()).ToList();
    //Создаем частичную копию
    var rows2 = rows1.Take(5);
    //Объединяем что бы коллекция содержала дубликаты
    var rows = rows1.Concat(rows2);
    

    //Получаем уникальные записи и индексируем их
    var results = rows.Distinct().Select((r, i)=> new {Index = i, Row = r});

    foreach(var r in results)
    Console.WriteLine(string.Format("Index: {0} Row: {1}", r.Index, r.Row));

    //если же необходимо проиндексировать изначальный список и получить первые элементы
    //а дубликаты исключить то можно поступить так

    var results2 = rows.OrderBy(x=>x)
    .Select((r,i)=>new {Index = i, Row = r})
    .GroupBy(x=>x.Row)
    .Select(x=>new {Index = x.First().Index, Row = x.Key});

    foreach(var r in results2)
    Console.WriteLine(string.Format("Index {0} Row {1}", r.Index, r.Row));

    I would like to know how to expand Select(), Distinct() and others have been available to include the following area of names System.Linq; by directive using

    example https://dotnetfiddle.net/RH1gHy




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2