回答編集履歴
2
追記
answer
CHANGED
@@ -7,4 +7,125 @@
|
|
7
7
|
匿名型と Distinct メソッド
|
8
8
|
[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)
|
9
9
|
|
10
|
-
不明点がある場合、やってみたがうまくいかない場合は連絡ください。
|
10
|
+
不明点がある場合、やってみたがうまくいかない場合は連絡ください。
|
11
|
+
|
12
|
+
**【追記】**
|
13
|
+
|
14
|
+
上に紹介した記事「匿名型と Distinct メソッド」の 1. の方法で Distinct メソッドによる重複排除ができるようにしたサンプルを書いておきます。これでは質問者さんの目的が果たせない場合は、どこをどう直すべきか具体的に連絡ください。
|
15
|
+
|
16
|
+
```
|
17
|
+
using System;
|
18
|
+
using System.Collections.Generic;
|
19
|
+
using System.Linq;
|
20
|
+
using System.Text;
|
21
|
+
using System.Threading.Tasks;
|
22
|
+
|
23
|
+
namespace ConsoleAppDistinct
|
24
|
+
{
|
25
|
+
public class Apple
|
26
|
+
{
|
27
|
+
public int Weight { get; set; }
|
28
|
+
public int Price { get; set; }
|
29
|
+
}
|
30
|
+
|
31
|
+
public class Orange
|
32
|
+
{
|
33
|
+
public int Weight { get; set; }
|
34
|
+
public int Price { get; set; }
|
35
|
+
}
|
36
|
+
|
37
|
+
public class AppleAndOrange : IEquatable<AppleAndOrange>
|
38
|
+
{
|
39
|
+
public string Name { get; set; }
|
40
|
+
public int Weight { get; set; }
|
41
|
+
public int Price { get; set; }
|
42
|
+
|
43
|
+
public bool Equals(AppleAndOrange other)
|
44
|
+
{
|
45
|
+
if (Object.ReferenceEquals(other, null)) return false;
|
46
|
+
|
47
|
+
if (Object.ReferenceEquals(this, other)) return true;
|
48
|
+
|
49
|
+
return Price.Equals(other.Price) && Weight.Equals(other.Weight);
|
50
|
+
}
|
51
|
+
|
52
|
+
// If Equals() returns true for a pair of objects
|
53
|
+
// then GetHashCode() must return the same value for these objects.
|
54
|
+
public override int GetHashCode()
|
55
|
+
{
|
56
|
+
return Price.GetHashCode() ^ Weight.GetHashCode();
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
class Program
|
61
|
+
{
|
62
|
+
static void Main(string[] args)
|
63
|
+
{
|
64
|
+
List<Apple> apples = new List<Apple>
|
65
|
+
{
|
66
|
+
new Apple { Price = 100, Weight = 100 },
|
67
|
+
new Apple { Price = 110, Weight = 110 },
|
68
|
+
new Apple { Price = 120, Weight = 120 },
|
69
|
+
new Apple { Price = 130, Weight = 130 },
|
70
|
+
new Apple { Price = 140, Weight = 140 }
|
71
|
+
};
|
72
|
+
|
73
|
+
List<Orange> oranges = new List<Orange>
|
74
|
+
{
|
75
|
+
new Orange { Price = 100, Weight = 110 },
|
76
|
+
new Orange { Price = 105, Weight = 100 },
|
77
|
+
new Orange { Price = 120, Weight = 120 },
|
78
|
+
new Orange { Price = 130, Weight = 125 },
|
79
|
+
new Orange { Price = 140, Weight = 140 }
|
80
|
+
};
|
81
|
+
|
82
|
+
List<AppleAndOrange> applesAndOranges = new List<AppleAndOrange>();
|
83
|
+
|
84
|
+
foreach(Apple a in apples)
|
85
|
+
{
|
86
|
+
applesAndOranges.Add(new AppleAndOrange { Name = "Apple", Price = a.Price, Weight = a.Weight });
|
87
|
+
}
|
88
|
+
|
89
|
+
foreach (Orange o in oranges)
|
90
|
+
{
|
91
|
+
applesAndOranges.Add(new AppleAndOrange { Name = "Orange", Price = o.Price, Weight = o.Weight });
|
92
|
+
}
|
93
|
+
|
94
|
+
foreach (AppleAndOrange x in applesAndOranges)
|
95
|
+
{
|
96
|
+
Console.WriteLine($"Name: {x.Name}, Price: {x.Price}, Weight: {x.Weight}");
|
97
|
+
}
|
98
|
+
|
99
|
+
Console.WriteLine("-------------------------");
|
100
|
+
|
101
|
+
foreach (AppleAndOrange x in applesAndOranges.Distinct())
|
102
|
+
{
|
103
|
+
Console.WriteLine($"Name: {x.Name}, Price: {x.Price}, Weight: {x.Weight}");
|
104
|
+
}
|
105
|
+
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
```
|
110
|
+
|
111
|
+
実行結果は以下の通りとなります。
|
112
|
+
|
113
|
+
Name: Apple, Price: 100, Weight: 100
|
114
|
+
Name: Apple, Price: 110, Weight: 110
|
115
|
+
Name: Apple, Price: 120, Weight: 120
|
116
|
+
Name: Apple, Price: 130, Weight: 130
|
117
|
+
Name: Apple, Price: 140, Weight: 140
|
118
|
+
Name: Orange, Price: 100, Weight: 110
|
119
|
+
Name: Orange, Price: 105, Weight: 100
|
120
|
+
Name: Orange, Price: 120, Weight: 120
|
121
|
+
Name: Orange, Price: 130, Weight: 125
|
122
|
+
Name: Orange, Price: 140, Weight: 140
|
123
|
+
------------------
|
124
|
+
Name: Apple, Price: 100, Weight: 100
|
125
|
+
Name: Apple, Price: 110, Weight: 110
|
126
|
+
Name: Apple, Price: 120, Weight: 120
|
127
|
+
Name: Apple, Price: 130, Weight: 130
|
128
|
+
Name: Apple, Price: 140, Weight: 140
|
129
|
+
Name: Orange, Price: 100, Weight: 110
|
130
|
+
Name: Orange, Price: 105, Weight: 100
|
131
|
+
Name: Orange, Price: 130, Weight: 125
|
1
訂正
answer
CHANGED
@@ -5,6 +5,6 @@
|
|
5
5
|
List<Apple> と List<Orange> から List<AppleAndOrange> を作るということでよければ、以下の記事の 1 または 2 いずれかの方法で Distinct メソッドで「重さ」と「値段」の重複をチェックできるようにしていかがですか?
|
6
6
|
|
7
7
|
匿名型と Distinct メソッド
|
8
|
-
http://surferonwww.info/BlogEngine/post/2015/12/08/anonymous-type-and-distinct-method.aspx
|
8
|
+
[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)
|
9
9
|
|
10
10
|
不明点がある場合、やってみたがうまくいかない場合は連絡ください。
|