public class ListPoint
{
public int Point0X { get; set; }
public int Point0Y { get; set; }
public int Point1X { get; set; }
public int Point1Y { get; set; }
public int Point2X { get; set; }
public int Point2Y { get; set; }
public int Point3X { get; set; }
public int Point3Y { get; set; }
public string tm { get; set; }
}
public class listPoint2
{
public int Point0X2 { get; set; }
public int Point0Y2 { get; set; }
public int Point1X2 { get; set; }
public int Point1Y2 { get; set; }
public int Point2X2 { get; set; }
public int Point2Y2 { get; set; }
public int Point3X2 { get; set; }
public int Point3Y2 { get; set; }
public string tm2 { get; set; }
}
以上是两个不同类
我将这里去重了
//合并去重
List Result = listPoints1.Union(listPoints2).ToList();
List Result2 = listPoints11.Union(listPoints22).ToList();
但我现在想要 result+result2 两个不同类的数据并且是去重的不知如何获取
result+result2 是不同的类,所以即使求出结果我们也只能得到一个
List
至于去重,去重需要比较,比较两个object大小,需要实现对象比较大小的接口
原理讲完,我们上代码List<int> x = new List<int> { 1, 2, 3, 4 }; List<string> y = new List<string>{"1", "2", "3", "4", "5"}; List<object> z = new List<object>(); z.AddRange(x.Cast<object>()); z.AddRange(y.Cast<object>()); var r = z.Distinct(new 自定义比较器()).ToList(); Console.ReadKey(); class 自定义比较器 : IEqualityComparer<object> { public bool Equals(object x, object y) { //一个是int,一个string,我们按字面值比较,比如都变成int然后比较大小 //或者都变成string比较大小 int? _x=null; int? _y=null; if (x is string _strx) { if (int.TryParse(_strx, out var _xx)) { _x = _xx; } } if (x is int _intx) { _x = _intx; } if (y is string _stry) { if (int.TryParse(_stry, out var _xx)) { _y = _xx; } } if (y is int _inty) { _y = _inty; } if (_x.HasValue && _y.HasValue) { return _x.Equals(_y); } return x.Equals(y); } public int GetHashCode(object obj) { if (obj is string _obj) { if (int.TryParse(_obj, out var t)) { return t.GetHashCode(); } } return obj.GetHashCode(); } }
结果是---------1,2,3,4,“5”
4个int,一个字符串
不同类怎么去重,属性都不一样没法去重
除非你类1和类2都继承自同一个基类,并且要去重的属性是在基类里定义的
https://blog.csdn.net/weixin_42126668/article/details/112947681
请参考一下这篇文章