检查列表中列表中的重复objectIds

时间:2018-10-15 13:01:38

标签: c# list linq object for-loop

我正在竭尽全力解决这个问题: 我当前正在浏览包含引用的“ buildingObjects”列表和包含“ ObjectId”的带有“ assets”的另一个列表。

            var typeBuildingPart = new List<IfcBuildingPart> {
            new IfcBuildingPart{
                BIMId = "iojeofhwofh308ry308hi32r08yrh",
                Reference = "234",
                Assets = new List<IfcAsset> {
                    new IfcAsset{
                        ObjectID = 6111838616,
                    }
                }
            },
                            new IfcBuildingPart{
                Reference = "235",
                Assets = new List<IfcAsset> {
                    new IfcAsset{
                        ObjectID = 6111838616,
                    }
                }
            },
               new IfcBuildingPart{
                Reference = "235",
                Assets = new List<IfcAsset> {
                    new IfcAsset{
                        ObjectID = 6111838616,
                    }
                }
            },
        };

目标是遍历引用和objectId,以检查是否存在objectId可能具有不同引用的实例(如代码片段所示,其中资产的objectID相同,但它们不相同)具有相同的参考,然后最后保留被引用最多的参考。 我尝试并做多个for循环,但最终失败了。有什么好的linq方法来解决这个问题,或者有什么指针吗? 谢谢!

应该有多个资产,但是我仅在每个建筑部件中选择了一个资产来演示ObjectId问题

最终结果应该是删除参考编号为234的ifcasset。

2 个答案:

答案 0 :(得分:0)

首先过滤列表,以仅包括至少与您的条件匹配的IfcAsset的元素。对Reference进行分组并计算发生的次数。这样,您就可以按计数排序并取第一个。

var data = new List<Foo> {
    new Foo{Ref="A", Bars= new List<Bar>{ new Bar {Id="Good" } } },
    new Foo{Ref="B", Bars= new List<Bar>{ new Bar {Id="NotMe" } } },
    new Foo{Ref="C", Bars= new List<Bar>{ new Bar {Id="Good" } } },
    new Foo{Ref="C", Bars= new List<Bar>{ new Bar {Id="Good" } } }
};

var result = data.Where(x => x.Bars.Any(b => b.Id == "Good"))
                 .GroupBy(x => x.Ref)
                 .Select(g => new { count = g.Count(), item=g.First() })
                 .OrderByDescending(x=> x.count)
                 .Select(x=> x.item)
                 .First();

答案 1 :(得分:0)

查看下面的代码是否有帮助。我创建了两个字典,它们从对象映射到引用,然后再映射到对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
         static void Main(string[] args)
        {
            var typeBuildingPart = new List<IfcBuildingPart> {
                new IfcBuildingPart{
                    BIMId = "iojeofhwofh308ry308hi32r08yrh",
                    Reference = "234",
                    Assets = new List<IfcAsset> {
                        new IfcAsset{
                            ObjectID = 6111838616,
                        }
                    }
                },
                                new IfcBuildingPart{
                    Reference = "235",
                    Assets = new List<IfcAsset> {
                        new IfcAsset{
                            ObjectID = 6111838616,
                        }
                    }
                },
                   new IfcBuildingPart{
                    Reference = "235",
                    Assets = new List<IfcAsset> {
                        new IfcAsset{
                            ObjectID = 6111838616,
                        }
                    }
                }
             };

            var dictRefToObj = typeBuildingPart.GroupBy(x => x.Reference, y => y.Assets.Select(z => z.ObjectID))
                .ToDictionary(x => x.Key, y => y.SelectMany(z => z).GroupBy(a => a).Select(a => new { obj = a.Key, count = a.Count()}).ToList());


             var dictObjToRef = dictRefToObj.Select(x => x.Value.Select(y => new { reference = x.Key, obj = y.obj, count = y.count })).SelectMany(z => z)
                .GroupBy(x => x.obj, y => new { reference = y.reference, count = y.count})
                .ToDictionary(x => x.Key, y => new { total = y.Select(z => z.count).Sum(), references = y.Select(z => new { reference = z.reference, count = z.count}).ToList()});


        }
    }
    public class IfcBuildingPart
    {
        public string BIMId { get; set; }
        public string Reference { get; set; }
        public List<IfcAsset> Assets { get; set; }
    }
    public class IfcAsset
    {
        public long ObjectID { get; set; }
    }
}