如何比较具有相似属性的两个截然不同的对象 [英] How to compare two distinctly different objects with similar properties

查看:49
本文介绍了如何比较具有相似属性的两个截然不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一切都在 C# 中,使用 .NET 2.0.

This is all in C#, using .NET 2.0.

我有两个对象列表.它们不是相关的对象,但它们确实有某些东西可以比较的共同点,例如基于 Guid 的唯一标识符.这两个列表需要被另一个只包含 Guid 的列表过滤,该列表可能匹配也可能不匹配ID 包含在前两个列表中.

I have two lists of objects. They are not related objects, but they do have certain things in common that can be compared such as a Guid based unique identifer. These two lists need be filtered by another list which just contains Guid's which may or may not match up with the ID's contained in the first two lists.

我想过将每个对象列表转换为对象"并按以下顺序排序的想法那个,但我不确定一旦它被转换,我就能访问 ID 属性,而且我认为对两个列表进行排序的方法应该在知道什么是愚蠢的要排序的列表是.

I have thought about the idea of casting each object list to just 'object' and sorting by that, but I'm not sure that I'll be able to access the ID property once it's cast, and I'm thinking that the method to sort the two lists should be somewhat dumb in knowing what the list to be sorted is.

引入每个对象列表以便可以根据仅包含 ID 的列表对其进行排序的最佳方法是什么?

What would be the best way to bring in each object list so that it can be sorted against the list with only the ID's?

推荐答案

你应该让每个不同的对象实现一个公共接口.然后为该接口创建一个 IComparer 并在您的排序中使用它.

You should make each of your different objects implement a common interface. Then create an IComparer<T> for that interface and use it in your sort.

这篇关于如何比较具有相似属性的两个截然不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆