首页 > 编程语言 >C# 两个列表取交集并集差集,比较复杂对象列表

C# 两个列表取交集并集差集,比较复杂对象列表

时间:2022-09-29 15:34:45浏览次数:51  
标签:return 并集 Area C# PosSaleReportDataShopComparer 列表 dayDiff public JzDate


public class PosSaleReportDataShopComparer: IEqualityComparer<PosSaleReportData> { private double dayDiff=365; public PosSaleReportDataShopComparer(double day) { dayDiff = day; } public bool Equals(PosSaleReportData x, PosSaleReportData y) { try { var result1 = x.ShoppeCode == y.ShoppeCode; var second = Math.Abs((x.JzDate - y.JzDate.AddDays(dayDiff)).TotalSeconds); var result2 = Math.Abs((x.JzDate - y.JzDate.AddDays(dayDiff)).TotalSeconds) < 1|| Math.Abs((y.JzDate - x.JzDate.AddDays(dayDiff)).TotalSeconds) < 1; if(result1&&result2) { if(x.JzDate>y.JzDate) { x.LSsTotal = y.SsTotal; x.Area = y.Area; x.AreaAverage = y.AreaAverage; } else { y.LSsTotal = x.SsTotal; y.LArea = x.Area; y.LAreaAverage = x.AreaAverage; } } return result1&&result2; } catch (Exception ex) { return base.Equals(x); } } public int GetHashCode(PosSaleReportData obj) { return obj.ShoppeCode.GetHashCode(); } }
写一个继承IEqualityComparer接口的类PosSaleReportDataShopComparer,然后再调用Intersect方法时传入该类: list1.Intersect(list2, new PosSaleReportDataShopComparer()).ToList()

标签:return,并集,Area,C#,PosSaleReportDataShopComparer,列表,dayDiff,public,JzDate
From: https://www.cnblogs.com/long7long/p/16741750.html

相关文章