如何计算列表中共享相同接口的不同对象?

发布于 2024-11-09 11:59:44 字数 1167 浏览 0 评论 0原文

我有一个对象 (MyObj),它本身将保存各种类型的其他对象的列表,我想在将它们添加到 MyObj 时对它们进行计数。无论如何,这是简单的解释......

我有一个所有子对象都同意的接口(MyInterface)。 MyObj 有一个 List(Of MyInterface) 属性,所有子对象都添加到该属性中。 MyInterface 将公开一个属性,让我弄清楚每个对象是什么子类型(子对象根本不继承自 MyObj)。

但我想在这些子对象添加到列表时对它们进行计数,并且我正在尝试找到一种好方法来做到这一点。我不需要担心计数递减,因为我将模仿 String 的行为,并在发生变化时创建一个新的 MyObj 实例,因此我的所有计数都将从 0 开始这会对垃圾收集器造成一些影响,但我认为这将允许更简单(更安全)的代码。

我能想到的计算对象的唯一明智的方法是 MyObj 中的一个非常大的结构,它使用字节来保存计数(我永远不会在列表中拥有超过 255 个给定的子对象) MyObj)。但是,即使使用字节,该结构在内存中也会占用大约 100-200 字节(我有那么多子对象),并且我预计也会有相当数量的 MyObj 副本在运行。

我还需要一个大的 Select Case 来了解添加新子对象时要增加的计数属性。这似乎有点难看,尽管我已经多次使用过这种方法。

我想知道的是,是否有某种方法可以查询列表并仅对特定类型的对象进行计数,而不是在添加到列表中时对每个对象进行计数?我不需要将其存储在任何地方,因为它是动态的,例如查询数据库并请求特定列或记录类型的计数。

我怀疑 Linq 可以做到这一点,但 Linq 也很慢。还有其他方法吗?也许是某种谓词?


编辑:我想我想在 Linq 中得到类似的东西,但我对如何在 VB 中格式化它感到有点困惑(我不是 C# 人):

From i in MyObj.MyList Group i by i.GetType into g Let c as Int32 = g.Count()

到目前为止就是这样。我仍在谷歌搜索,但我得到了太多的 C# 和 SQL 参考资料。

I have an object (MyObj) that itself will hold a List of other objects of various types and I want to count them as they are added to MyObj. That's the simple explanation, anyways...

I have an Interface (MyInterface) that all sub-objects agree to. MyObj has a List(Of MyInterface) property that all the sub-objects are added to. MyInterface will expose a property that lets me figure out what subtype each object is (the sub-objects do not inherit from MyObj at all).

But I want to count these sub-objects as they are added to the list, and I'm trying to find a good way to do it. I don't need to worry about decrementing the count, as I am going to mimic the behavior of String and just create a new instance of MyObj if it ever changes, so all my counts will start from 0. This'll hit the garbage collector a bit, but I think this will allow for simpler (and safer) code.

The only sane way I can think of to count objects is a very large structure in MyObj that uses bytes to hold the count (I will never have more than 255 of any given sub-object in the list in MyObj). But, even using bytes, this structure will be about 100-200 bytes big in memory (I have that many sub-objects), and I anticipate having a fair amount of MyObj copies running around, too.

I'll also need a large Select Case to know which count property to increment when a new sub-object is added. This seems to be a bit ugly, though I've used this approach several times already.

What I am wondering is, instead of counting each object as it is added to the list, is there some way to query the list and count only the objects of a specific type? I wouldn't need to store this anywhere, since it would be dynamic, like querying a database and asking for a count of a specific column or type of record.

I suspect Linq can do this, but Linq is also quite slow. Are there other approaches? Perhaps a predicate of some kind?

EDIT: I tink I want something like this in Linq, but I'm a bit confused on how to format it in VB (I am not a C# guy):

From i in MyObj.MyList Group i by i.GetType into g Let c as Int32 = g.Count()

And that's it so far. I am still googling, but I am getting way too many C# and SQL references.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2024-11-16 11:59:44
MyObj.MyList.OfType(Of Int32)().Count()
MyObj.MyList.OfType(Of Int32)().Count()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文