【问题标题】:How to get specific set of objects from list of objects如何从对象列表中获取特定的对象集
【发布时间】:2014-07-01 22:26:39
【问题描述】:
List<objects> MyObjects = GetobjectsfromList(otherlist);

我正在使用的 MyObjects 列表具有多个属性

String name;

String locationInfo;

String otherObjectName;

DateTime date1;

DateTime date2;

(等等)

MyObjects 中包含的内容如下:

Obj1 (name1, location1, otherobjectname1, date1, date2)

Obj2 (name2, location2, otherobjectname1, date4, date7)

Obj3 (name3, location3, otherobjectname1, date6, date9)

Obj4 (name4, location6, otherobjectname2, date1, date2)

Obj5 (name5, location7, otherobjectname2, date1, date2)

(总共大约 2600 条记录,这些属性使每条记录都是唯一的)

基本上所有的 ObJ 对象都至少有一个属性,使它们在集合中是唯一的。因此,使用我尝试过的任何 groupby、distinct 或任何其他 linq .where 子句总是能让我恢复整个集合,因为每条记录都是真正独一无二的。

我需要的是从整个对象集合中获取每个对象中的一个,以获得对象上的不同属性……即另一个对象名称。看看一个有 3 条记录,另一个有 2 条记录(我已经从这些 otherobjectname 制作了一个哈希集,只有 975 条记录)。

我需要从这个集合中得到的只是一个新的 MyObjects 集合,其中每个对象名我只有一个,我不在乎它是哪条记录。

所以返回包含这些的新列表:

Obj1 (name1, location1, otherobjectname1, date1, date2) (I do not care which of the 3)

Obj4 (name4, location6, otherobjectname2, date1, date2) (I do not care which of the 2)

对于集合中每个唯一的 otherobjectname 以此类推

对象上的属性之一只有一个唯一记录 有没有办法做到这一点?抱歉,我不能真正发布示例代码,由于安全规则,我尝试在不使用任何特定内容的情况下尽我所能写出来。

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    您可以使用DistinctBy 方法(它不是标准的Linq 方法,但您可以在MoreLinqLinq.Extras 中找到它的实现。

    var distinct = MyObjects.DistinctBy(x => w.OtherObjectName);
    

    或者,如果您愿意,您可以创建一个仅比较 OtherObjectName 属性的自定义相等比较器,并将其传递给 Distinct

    class MyObjectComparerByOtherObjectName : IEqualityComparer<MyObject>
    {
        public bool Equals(MyObject x, MyObject y)
        {
            return x.OtherObjectName == y.OtherObjectName;
        }
    
        public bool GetHashCode(MyObject x)
        {
            return x.OtherObjectName != null ? x.OtherObjectName.GetHashCode() : 0;
        }
    }
    
    ...
    
    var distinct = MyObjects.Distinct(new MyObjectComparerByOtherObjectName());
    

    【讨论】:

      【解决方案2】:

      您可以通过otherObjectName 进行分组,然后从组中选择第一个,如下所示:

      static void Main(string[] args)
      {
          List<MyObject> objects = new List<MyObject> {
              new MyObject { name = "name1", locationInfo = "location1", otherObjectName = "otherobjectname1" },
              new MyObject { name = "name2", locationInfo = "location2", otherObjectName = "otherobjectname1" },
              new MyObject { name = "name3", locationInfo = "location3", otherObjectName = "otherobjectname1" },
              new MyObject { name = "name4", locationInfo = "location6", otherObjectName = "otherobjectname2" },
              new MyObject { name = "name5", locationInfo = "location7", otherObjectName = "otherobjectname2" },
          };
      
          var query = objects.GroupBy(o => o.otherObjectName)
              .Select(g => g.First());
      
          foreach(var o in query)
              Console.WriteLine("{0} {1}", o.name,  o.otherObjectName);
      }
      

      这会返回:

      name1 otherobjectname1
      name4 otherobjectname2
      

      【讨论】:

        猜你喜欢
        • 2022-08-15
        • 1970-01-01
        • 1970-01-01
        • 2019-08-24
        • 2019-02-06
        • 1970-01-01
        • 2018-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多