我的解决方案使用 LINQ 和自定义的 Comparer 实例来比较 Parameters 成员,并对 Parameters 中的项目做出一些假设。但是,如果这些假设是正确的,您可以通过使用像Max's answer中建议的 Param 属性来简化实现。这是它的要点:
private static List<Object1> MergeLists(List<Object1> list1, List<Object1> list2)
{
var parameterComparer = new ParameterComparer();
var distinctParameters = list1.Select(o => o.Parameters)
.Concat(list2.Select(o => o.Parameters))
.Distinct(parameterComparer);
return (from p in distinctParameters
let o1 = list1.SingleOrDefault(o => parameterComparer.Equals(p, o.Parameters))
let o2 = list2.SingleOrDefault(o => parameterComparer.Equals(p, o.Parameters))
let result = o2 ?? o1
select result).ToList();
}
这是一个更完整的测试驱动答案。首先,Object1 声明——我添加了一个辅助构造函数以使声明更简洁:
public class Object1
{
public TimeSpan? Time { get; set; }
public List<string> Parameters { get; set; }
public Object1(TimeSpan? time, params string[] parameters)
{
Time = time;
Parameters = parameters.ToList();
}
}
接下来是测试方法。我定义了 Object1Comparer 以使测试的实现更简单——解决方案不需要它。
[TestMethod]
public void MergeListsTest()
{
// Arrange
var list1 = new List<Object1>
{
new Object1(null, "1", "1"),
new Object1(null, "1", "2"),
new Object1(null, "1", "3"),
new Object1(new TimeSpan(0, 0, 30), "2", "5")
};
var list2 = new List<Object1>
{
new Object1(new TimeSpan(0, 1, 20), "1", "1"),
new Object1(new TimeSpan(0, 0, 51), "1", "2"),
};
var expected = new List<Object1>
{
new Object1(new TimeSpan(0, 1, 20), "1", "1"),
new Object1(new TimeSpan(0, 0, 51), "1", "2"),
new Object1(null, "1", "3"),
new Object1(new TimeSpan(0, 0, 30), "2", "5")
};
// Act
List<Object1> actual = MergeLists(list1, list2);
// Assert
// Note: need to order the actual result to use CollectionAssert.AreEqual()
List<Object1> orderedActual = actual.OrderBy(o => string.Join(";", o.Parameters)).ToList();
CollectionAssert.AreEqual(expected, orderedActual, new Object1Comparer());
}
public class Object1Comparer : IComparer, IComparer<Object1>
{
public int Compare(Object1 x, Object1 y)
{
if (x.Time == null && y.Time == null) return 0;
if (x.Time == null || y.Time == null) return -1;
int timeComparison = TimeSpan.Compare(x.Time.Value, y.Time.Value);
if (timeComparison != 0) return timeComparison;
if (x.Parameters == null && y.Parameters == null) return 0;
if (x.Parameters == null || y.Parameters == null) return -1;
if (x.Parameters.SequenceEqual(y.Parameters)) return 0;
return -1;
}
public int Compare(object x, object y)
{
if (x is Object1 && y is Object1)
return Compare(x as Object1, y as Object1);
return -1;
}
}
最后,这是 MergeLists 的实现:
public class ParameterComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
if (x == null && y == null) return true;
if (x == null || y == null) return false;
return x.SequenceEqual(y);
}
public int GetHashCode(List<string> obj)
{
if (obj == null) throw new ArgumentNullException("obj");
// Note: this is not a safe way to get a hash code,
// but if you're sure that the members are always ordered
// and will never contain a semi-colon, then it will work.
return string.Join(";", obj).GetHashCode();
}
}
private static List<Object1> MergeLists(List<Object1> list1, List<Object1> list2)
{
var parameterComparer = new ParameterComparer();
var distinctParameters = list1.Select(o => o.Parameters)
.Concat(list2.Select(o => o.Parameters))
.Distinct(parameterComparer);
return (from p in distinctParameters
let o1 = list1.SingleOrDefault(o => parameterComparer.Equals(p, o.Parameters))
let o2 = list2.SingleOrDefault(o => parameterComparer.Equals(p, o.Parameters))
let result = o2 ?? o1
select result).ToList();
}