【问题标题】:Delete duplicate elements from list by Identifier string (C#)通过标识符字符串从列表中删除重复元素(C#)
【发布时间】:2020-05-25 06:11:12
【问题描述】:

我有向列表添加元素的方法 这是代码

 public static List<InputDevice> GetAudioInputDevices()
    {
        var inputs = new List<InputDevice>();
        var enumerator = new MMDeviceEnumerator();
        var devicesAudio = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
        foreach (var device in devicesAudio)
        {
            inputs.Add(new InputDevice()
            {
                Name = device.FriendlyName,
                Status = device.State.ToString(),
                DeviceId = device.ID,
                Identifier = device.FriendlyName.Replace(" ", "").ToUpper()
            });
        }


        return inputs;
    }

但有时我可以在 Identifier 中有重复项

如何在返回时返回列表而不重复?

【问题讨论】:

  • 返回输入.DistinctBy(x => x.Identifier ).ToList();
  • 什么是DistinctBy?我猜收藏只有Distinct@JimmyN
  • 抱歉,我忘记了,DistincyBy 是通过github.com/morelinq/MoreLINQ 提供的。或者你可以这样做:inputs.GroupBy(x => x.Identifier).Select(g => g.First()).ToList();
  • 重复的标识符是否可能具有不同的状态(活动/不存在/未插入)?如果您只需要 Active 的,您可以更改“var devicesAudio = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);”中的第二个参数只返回状态等于 DeviceState.Active 的设备
  • 您要求Identifier是关键字段吗?你这里提到的duplicate Identifier,那么此时Name等字段都一样吗?这与devicesAudio获取的数据有关。如果出现相同的 Identifier,您要选择显示哪些数据?也许你应该详细描述一下。

标签: c# asp.net .net asp.net-core


【解决方案1】:

有几种方法可以做到这一点,如果项目已经在列表中,您可以跳过添加项目:

        foreach (var device in devicesAudio)
        {
            string identifier = device.FriendlyName.Replace(" ", "").ToUpper();

            if (inputs.Any(input => input.Identifier == identifier))
                continue;

            inputs.Add(new InputDevice()
            {
                Name = device.FriendlyName,
                Status = device.State.ToString(),
                DeviceId = device.ID,
                Identifier = identifier
            });
        }

或者您可以按foreach 之后的标识符对列表进行分组,如下所示:

inputs = inputs.GroupBy(i => i.Identifier)
    .Select(i => new InputDevice()
    {
        Identifier = i.Key,
        Status = i.First().Status,
        DeviceId = i.First().DeviceId,
        Name = i.First().Name
    }).ToList();

这真的取决于你需要如何处理重复的。

希望对你有帮助!

【讨论】:

    【解决方案2】:

    为了使其更快,您可以使用 HashSet(HashSet 的包含复杂度为 o(1))并在每个循环中询问输入列表中是否已经存在特定标识符。

    public static List<InputDevice> GetAudioInputDevices()
    {
        var inputs = new List<InputDevice>();
        var enumerator = new MMDeviceEnumerator();
        var devicesAudio = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
    
        var usedIdentifiers = new HashSet<string>();
        foreach (var device in devicesAudio)
        {
            var identifier = device.FriendlyName.Replace(" ", "").ToUpper();
            if (usedIdentifiers.Contains(identifier))
                continue;
    
            inputs.Add(new InputDevice()
            {
                Name = device.FriendlyName,
                Status = device.State.ToString(),
                DeviceId = device.ID,
                Identifier = identifier
            });
    
            usedIdentifiers.Add(identifier);
        }
    
    
        return inputs;
    }
    

    【讨论】:

      【解决方案3】:

      最好的办法,我喜欢的是这个

          public static List<InputDevice> GetAudioInputDevices()
      {
          var inputs = new List<InputDevice>();
          var enumerator = new MMDeviceEnumerator();
          var devicesAudio = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.All);
          inputs = devicesAudio.GroupBy(d => d.FriendlyName.Replace(" ", "").ToUpper()).Select(g => g.First())
              .Select(d => new InputDevice()
              {
                  Name = d.FriendlyName,
                  Status = d.State.ToString(),
                  DeviceId = d.ID,
                  Identifier = d.FriendlyName.Replace(" ", "").ToUpper()
              }).ToList();
          return inputs;
      }
      

      【讨论】:

        【解决方案4】:

        查看关于HashSet的网站信息。

        【讨论】:

          猜你喜欢
          • 2019-10-16
          • 1970-01-01
          • 1970-01-01
          • 2014-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          相关资源
          最近更新 更多