【问题标题】:How to select distinct items from a list如何从列表中选择不同的项目
【发布时间】:2016-12-19 16:40:29
【问题描述】:

我有这门课

class Test
    {
        public string Property { get; set; }
        public string ID { get; set; }
        public List<string> MyProperty { get; set; } 

    }

我创建了它的一个实例

 List<Test> t = new List<Test>() {
                new Test() {
                     ID = "1",
                      Property = "2"

                },
                new Test() {
                     ID = "2",
                      Property = "3"
                },
                new Test() {
                     ID = "2",
                     Property = "5"
                }

            };

我想要一个列表,其中包含按 ID 过滤的不同元素以及 public List MyProperty { get;放; } 应填写 public string Property { get;放; } 数据。

所以最终的结果应该是

List<Test> = {
   1.   ID = "1",List<MyProperty> = "2"
   2.   ID = "2",List<MyProperty> = "2"                        

};

【问题讨论】:

  • List&lt;MyProperty&gt; 你的意思是List&lt;string&gt; MyPropertyList 吗?您不能使用变量的名称作为类型。

标签: c# .net c#-4.0


【解决方案1】:

您可以使用GroupByFirst 删除重复项:

t.GroupBy(x => x.Id)
    .Select(g => g.First())
    .ToList();

【讨论】:

    【解决方案2】:

    我会使用GroupBy() LINQ 扩展:

    t.GroupBy(x => x.ID)
     .Select(x => new Test {
        ID = x.Key,
        MyProperty = x.Select(y => y.Property).ToList()
     })
     .ToList();
    

    GroupBy 的参数是您要分组的键,所以在您的情况下是 ID。

    Select 然后会将它们投影到新的Test

    这里有一些有用的链接:

    https://msdn.microsoft.com/en-us/library/bb545971.aspx

    https://msdn.microsoft.com/en-us/library/bb534304(v=vs.110).aspx

    结果将是:

    [
        {
            "ID": "1",
            "MyProperty": [ "2" ],
            "Property": null
        },
        {
            "ID": "2",
            "MyProperty": [ "3", "5" ],
            "Property": null
        },
    ]
    

    【讨论】:

      【解决方案3】:
      t.Distinct(new TestComparer());
      

      其中 TestComparer 是您的比较器的实现。 这是sample

      // Custom comparer for the Test class
      class ProductComparer : IEqualityComparer<Test>
      {
          // Tests are equal if their IDs are equal.
          public bool Equals(Test x, Test y)
          {
              //Check whether the compared objects reference the same data.
              if (Object.ReferenceEquals(x, y)) return true;
      
              //Check whether any of the compared objects is null.
              if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                  return false;
      
              //Check whether the products' properties are equal.
              return x.Id == y.Id;
          }
      
          // If Equals() returns true for a pair of objects 
          // then GetHashCode() must return the same value for these objects.
      
          public int GetHashCode(Test test)
          {
              //Check whether the object is null
              if (Object.ReferenceEquals(test, null)) return 0;
      
              //Get hash code for the Name field if it is not null.
              int hashId = test.Id == null ? 0 : test.Id.GetHashCode();
      
              //Calculate the hash code for the test.
              return hashId;
      
              //Should be enough, but you can merge hashcodes of other fields in some way, for example:
              //int hashProperty = test.Property == null ? 0 : test.Property.GetHashCode();
              //return hashId ^ hashProperty;
          }
      }
      

      【讨论】:

      • 请在此处插入示例... :)
      猜你喜欢
      • 1970-01-01
      • 2015-03-12
      • 2016-04-18
      • 1970-01-01
      • 2016-07-07
      • 2017-12-23
      • 1970-01-01
      • 2012-01-16
      • 2018-11-13
      相关资源
      最近更新 更多