1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace wpfDistinct
8 {
9 class DataCompare : IEqualityComparer<DateTime>
10 {
11
12 public bool Equals(DateTime x, DateTime y)
13 {
14 return x.Hour == y.Hour;
15 }
16
17 public int GetHashCode(DateTime obj)
18 {
19 return (obj.Year + obj.Month + obj.Day + obj.Hour).ToString().GetHashCode();
20
21 }
22 }
23 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15
16 namespace wpfDistinct
17 {
18 /// <summary>
19 /// MainWindow.xaml 的交互逻辑
20 /// </summary>
21 public partial class MainWindow : Window
22 {
23 public MainWindow()
24 {
25 InitializeComponent();
26
27 Random rand = new Random();
28 List<DateTime> list = new List<DateTime>();
29 for (int i = 0; i < 100; i++)
30 {
31 list.Add(new DateTime(2016, rand.Next(1, 12), rand.Next(1, 31), rand.Next(1, 12),rand.Next(1,59),rand.Next(1,59)));
32 }
33 if (null != list)
34 {
35 List<DateTime> list3 = list.Distinct(new DataCompare()).ToList();
36
37 if (null != list3)
38 {
39 List<List<DateTime>> collcetion = new List<List<DateTime>>();
40 foreach (DateTime time in list3)
41 {
42 collcetion.Add(list.Where(i => i.Month==time.Month&& i.Day == time.Day&&i.Hour==time.Hour).ToList());
43 }
44 }
45
46
47 }
48
49 }
50 }
51 }
Distinct 要实现IEqualityComparer<DateTime> 这个接口才能自定义
hashCode的精度到那个地方,就可以以那种精度进行去重