回答編集履歴
2
追記
test
CHANGED
@@ -17,3 +17,245 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
不明点がある場合、やってみたがうまくいかない場合は連絡ください。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
**【追記】**
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
上に紹介した記事「匿名型と Distinct メソッド」の 1. の方法で Distinct メソッドによる重複排除ができるようにしたサンプルを書いておきます。これでは質問者さんの目的が果たせない場合は、どこをどう直すべきか具体的に連絡ください。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
using System;
|
34
|
+
|
35
|
+
using System.Collections.Generic;
|
36
|
+
|
37
|
+
using System.Linq;
|
38
|
+
|
39
|
+
using System.Text;
|
40
|
+
|
41
|
+
using System.Threading.Tasks;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
namespace ConsoleAppDistinct
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
public class Apple
|
50
|
+
|
51
|
+
{
|
52
|
+
|
53
|
+
public int Weight { get; set; }
|
54
|
+
|
55
|
+
public int Price { get; set; }
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
public class Orange
|
62
|
+
|
63
|
+
{
|
64
|
+
|
65
|
+
public int Weight { get; set; }
|
66
|
+
|
67
|
+
public int Price { get; set; }
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
public class AppleAndOrange : IEquatable<AppleAndOrange>
|
74
|
+
|
75
|
+
{
|
76
|
+
|
77
|
+
public string Name { get; set; }
|
78
|
+
|
79
|
+
public int Weight { get; set; }
|
80
|
+
|
81
|
+
public int Price { get; set; }
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
public bool Equals(AppleAndOrange other)
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
if (Object.ReferenceEquals(other, null)) return false;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
if (Object.ReferenceEquals(this, other)) return true;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
return Price.Equals(other.Price) && Weight.Equals(other.Weight);
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// If Equals() returns true for a pair of objects
|
104
|
+
|
105
|
+
// then GetHashCode() must return the same value for these objects.
|
106
|
+
|
107
|
+
public override int GetHashCode()
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
return Price.GetHashCode() ^ Weight.GetHashCode();
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
class Program
|
120
|
+
|
121
|
+
{
|
122
|
+
|
123
|
+
static void Main(string[] args)
|
124
|
+
|
125
|
+
{
|
126
|
+
|
127
|
+
List<Apple> apples = new List<Apple>
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
new Apple { Price = 100, Weight = 100 },
|
132
|
+
|
133
|
+
new Apple { Price = 110, Weight = 110 },
|
134
|
+
|
135
|
+
new Apple { Price = 120, Weight = 120 },
|
136
|
+
|
137
|
+
new Apple { Price = 130, Weight = 130 },
|
138
|
+
|
139
|
+
new Apple { Price = 140, Weight = 140 }
|
140
|
+
|
141
|
+
};
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
List<Orange> oranges = new List<Orange>
|
146
|
+
|
147
|
+
{
|
148
|
+
|
149
|
+
new Orange { Price = 100, Weight = 110 },
|
150
|
+
|
151
|
+
new Orange { Price = 105, Weight = 100 },
|
152
|
+
|
153
|
+
new Orange { Price = 120, Weight = 120 },
|
154
|
+
|
155
|
+
new Orange { Price = 130, Weight = 125 },
|
156
|
+
|
157
|
+
new Orange { Price = 140, Weight = 140 }
|
158
|
+
|
159
|
+
};
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
List<AppleAndOrange> applesAndOranges = new List<AppleAndOrange>();
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
foreach(Apple a in apples)
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
applesAndOranges.Add(new AppleAndOrange { Name = "Apple", Price = a.Price, Weight = a.Weight });
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
foreach (Orange o in oranges)
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
applesAndOranges.Add(new AppleAndOrange { Name = "Orange", Price = o.Price, Weight = o.Weight });
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
foreach (AppleAndOrange x in applesAndOranges)
|
188
|
+
|
189
|
+
{
|
190
|
+
|
191
|
+
Console.WriteLine($"Name: {x.Name}, Price: {x.Price}, Weight: {x.Weight}");
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
Console.WriteLine("-------------------------");
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
foreach (AppleAndOrange x in applesAndOranges.Distinct())
|
202
|
+
|
203
|
+
{
|
204
|
+
|
205
|
+
Console.WriteLine($"Name: {x.Name}, Price: {x.Price}, Weight: {x.Weight}");
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
```
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
実行結果は以下の通りとなります。
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
Name: Apple, Price: 100, Weight: 100
|
226
|
+
|
227
|
+
Name: Apple, Price: 110, Weight: 110
|
228
|
+
|
229
|
+
Name: Apple, Price: 120, Weight: 120
|
230
|
+
|
231
|
+
Name: Apple, Price: 130, Weight: 130
|
232
|
+
|
233
|
+
Name: Apple, Price: 140, Weight: 140
|
234
|
+
|
235
|
+
Name: Orange, Price: 100, Weight: 110
|
236
|
+
|
237
|
+
Name: Orange, Price: 105, Weight: 100
|
238
|
+
|
239
|
+
Name: Orange, Price: 120, Weight: 120
|
240
|
+
|
241
|
+
Name: Orange, Price: 130, Weight: 125
|
242
|
+
|
243
|
+
Name: Orange, Price: 140, Weight: 140
|
244
|
+
|
245
|
+
------------------
|
246
|
+
|
247
|
+
Name: Apple, Price: 100, Weight: 100
|
248
|
+
|
249
|
+
Name: Apple, Price: 110, Weight: 110
|
250
|
+
|
251
|
+
Name: Apple, Price: 120, Weight: 120
|
252
|
+
|
253
|
+
Name: Apple, Price: 130, Weight: 130
|
254
|
+
|
255
|
+
Name: Apple, Price: 140, Weight: 140
|
256
|
+
|
257
|
+
Name: Orange, Price: 100, Weight: 110
|
258
|
+
|
259
|
+
Name: Orange, Price: 105, Weight: 100
|
260
|
+
|
261
|
+
Name: Orange, Price: 130, Weight: 125
|
1
訂正
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
匿名型と Distinct メソッド
|
14
14
|
|
15
|
-
http://surferonwww.info/BlogEngine/post/2015/12/08/anonymous-type-and-distinct-method.aspx
|
15
|
+
[http://surferonwww.info/BlogEngine/post/2015/12/08/anonymous-type-and-distinct-method.aspx](http://surferonwww.info/BlogEngine/post/2015/12/08/anonymous-type-and-distinct-method.aspx)
|
16
16
|
|
17
17
|
|
18
18
|
|