回答編集履歴
3
言語指定の誤りを修正
answer
CHANGED
@@ -1,71 +1,71 @@
|
|
1
|
-
こんな感じでどうでしょうか。
|
2
|
-
|
3
|
-
```
|
4
|
-
using System;
|
5
|
-
using System.Linq;
|
6
|
-
|
7
|
-
namespace LinqSample {
|
8
|
-
class Program {
|
9
|
-
static void Main(string[] args) {
|
10
|
-
var traders = new Trader[] {
|
11
|
-
new Trader() { Id = 1, CompanyNm = "株式会社ABC", Address = "北海道" },
|
12
|
-
new Trader() { Id = 2, CompanyNm = "株式会社DEF", Address = "青森" },
|
13
|
-
new Trader() { Id = 3, CompanyNm = "株式会社GHI", Address = "秋田" },
|
14
|
-
new Trader() { Id = 4, CompanyNm = "株式会社HIJ", Address = "岩手" },
|
15
|
-
new Trader() { Id = 5, CompanyNm = "株式会社KLM", Address = "山形" },
|
16
|
-
new Trader() { Id = 6, CompanyNm = "株式会社OPQ", Address = "宮城" },
|
17
|
-
};
|
18
|
-
|
19
|
-
var items = new ItemList[] {
|
20
|
-
new ItemList() { Id = 1, Price = 100, ExpiryDate = new DateTime(2000, 4, 1) },
|
21
|
-
new ItemList() { Id = 2, Price = 200, ExpiryDate = new DateTime(2005, 5, 1) },
|
22
|
-
new ItemList() { Id = 3, Price = 300, ExpiryDate = new DateTime(2010, 6, 1) },
|
23
|
-
new ItemList() { Id = 4, Price = 400, ExpiryDate = new DateTime(2015, 7, 1) },
|
24
|
-
new ItemList() { Id = 5, Price = 2020, ExpiryDate = new DateTime(2020, 8, 1) }
|
25
|
-
};
|
26
|
-
|
27
|
-
var result = traders.GroupJoin(
|
28
|
-
items,
|
29
|
-
trader => trader.Id,
|
30
|
-
item => item.Id,
|
31
|
-
(trader, item) => new { trader, item })
|
32
|
-
// シーケンスの展開
|
33
|
-
.SelectMany(traderAndItem => traderAndItem.item, (trader, item) => new {
|
34
|
-
Id = trader.trader.Id,
|
35
|
-
CompanyNm = trader.trader.CompanyNm,
|
36
|
-
Address = trader.trader.Address,
|
37
|
-
Price = item.Price,
|
38
|
-
ExpireDate = item.ExpiryDate
|
39
|
-
})
|
40
|
-
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
41
|
-
.ToList();
|
42
|
-
|
43
|
-
foreach (var o in result) {
|
44
|
-
Console.WriteLine($"Id={o.Id}, CompanyNm={o.CompanyNm}, Address={o.Address}, Price={o.Price}, ExpireDate={o.ExpireDate}");
|
45
|
-
}
|
46
|
-
}
|
47
|
-
}
|
48
|
-
|
49
|
-
class Trader {
|
50
|
-
public int Id { get; set; }
|
51
|
-
public string CompanyNm { get; set; }
|
52
|
-
public string Address { get; set; }
|
53
|
-
}
|
54
|
-
|
55
|
-
class ItemList {
|
56
|
-
public int Id { get; set; }
|
57
|
-
public int Price { get; set; }
|
58
|
-
public DateTime ExpiryDate { get; set; }
|
59
|
-
}
|
60
|
-
}
|
61
|
-
|
62
|
-
|
63
|
-
```
|
64
|
-
|
65
|
-
出力。
|
66
|
-
|
67
|
-
```
|
68
|
-
Id=3, CompanyNm=株式会社GHI, Address=秋田, Price=300, ExpireDate=10/06/01 0:00:00
|
69
|
-
Id=4, CompanyNm=株式会社HIJ, Address=岩手, Price=400, ExpireDate=15/07/01 0:00:00
|
70
|
-
Id=5, CompanyNm=株式会社KLM, Address=山形, Price=2020, ExpireDate=20/08/01 0:00:00
|
1
|
+
こんな感じでどうでしょうか。
|
2
|
+
|
3
|
+
```c#
|
4
|
+
using System;
|
5
|
+
using System.Linq;
|
6
|
+
|
7
|
+
namespace LinqSample {
|
8
|
+
class Program {
|
9
|
+
static void Main(string[] args) {
|
10
|
+
var traders = new Trader[] {
|
11
|
+
new Trader() { Id = 1, CompanyNm = "株式会社ABC", Address = "北海道" },
|
12
|
+
new Trader() { Id = 2, CompanyNm = "株式会社DEF", Address = "青森" },
|
13
|
+
new Trader() { Id = 3, CompanyNm = "株式会社GHI", Address = "秋田" },
|
14
|
+
new Trader() { Id = 4, CompanyNm = "株式会社HIJ", Address = "岩手" },
|
15
|
+
new Trader() { Id = 5, CompanyNm = "株式会社KLM", Address = "山形" },
|
16
|
+
new Trader() { Id = 6, CompanyNm = "株式会社OPQ", Address = "宮城" },
|
17
|
+
};
|
18
|
+
|
19
|
+
var items = new ItemList[] {
|
20
|
+
new ItemList() { Id = 1, Price = 100, ExpiryDate = new DateTime(2000, 4, 1) },
|
21
|
+
new ItemList() { Id = 2, Price = 200, ExpiryDate = new DateTime(2005, 5, 1) },
|
22
|
+
new ItemList() { Id = 3, Price = 300, ExpiryDate = new DateTime(2010, 6, 1) },
|
23
|
+
new ItemList() { Id = 4, Price = 400, ExpiryDate = new DateTime(2015, 7, 1) },
|
24
|
+
new ItemList() { Id = 5, Price = 2020, ExpiryDate = new DateTime(2020, 8, 1) }
|
25
|
+
};
|
26
|
+
|
27
|
+
var result = traders.GroupJoin(
|
28
|
+
items,
|
29
|
+
trader => trader.Id,
|
30
|
+
item => item.Id,
|
31
|
+
(trader, item) => new { trader, item })
|
32
|
+
// シーケンスの展開
|
33
|
+
.SelectMany(traderAndItem => traderAndItem.item, (trader, item) => new {
|
34
|
+
Id = trader.trader.Id,
|
35
|
+
CompanyNm = trader.trader.CompanyNm,
|
36
|
+
Address = trader.trader.Address,
|
37
|
+
Price = item.Price,
|
38
|
+
ExpireDate = item.ExpiryDate
|
39
|
+
})
|
40
|
+
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
41
|
+
.ToList();
|
42
|
+
|
43
|
+
foreach (var o in result) {
|
44
|
+
Console.WriteLine($"Id={o.Id}, CompanyNm={o.CompanyNm}, Address={o.Address}, Price={o.Price}, ExpireDate={o.ExpireDate}");
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
class Trader {
|
50
|
+
public int Id { get; set; }
|
51
|
+
public string CompanyNm { get; set; }
|
52
|
+
public string Address { get; set; }
|
53
|
+
}
|
54
|
+
|
55
|
+
class ItemList {
|
56
|
+
public int Id { get; set; }
|
57
|
+
public int Price { get; set; }
|
58
|
+
public DateTime ExpiryDate { get; set; }
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
出力。
|
66
|
+
|
67
|
+
```
|
68
|
+
Id=3, CompanyNm=株式会社GHI, Address=秋田, Price=300, ExpireDate=10/06/01 0:00:00
|
69
|
+
Id=4, CompanyNm=株式会社HIJ, Address=岩手, Price=400, ExpireDate=15/07/01 0:00:00
|
70
|
+
Id=5, CompanyNm=株式会社KLM, Address=山形, Price=2020, ExpireDate=20/08/01 0:00:00
|
71
71
|
```
|
2
refactor
answer
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
namespace LinqSample {
|
8
8
|
class Program {
|
9
9
|
static void Main(string[] args) {
|
10
|
-
|
10
|
+
var traders = new Trader[] {
|
11
11
|
new Trader() { Id = 1, CompanyNm = "株式会社ABC", Address = "北海道" },
|
12
12
|
new Trader() { Id = 2, CompanyNm = "株式会社DEF", Address = "青森" },
|
13
13
|
new Trader() { Id = 3, CompanyNm = "株式会社GHI", Address = "秋田" },
|
@@ -16,7 +16,7 @@
|
|
16
16
|
new Trader() { Id = 6, CompanyNm = "株式会社OPQ", Address = "宮城" },
|
17
17
|
};
|
18
18
|
|
19
|
-
|
19
|
+
var items = new ItemList[] {
|
20
20
|
new ItemList() { Id = 1, Price = 100, ExpiryDate = new DateTime(2000, 4, 1) },
|
21
21
|
new ItemList() { Id = 2, Price = 200, ExpiryDate = new DateTime(2005, 5, 1) },
|
22
22
|
new ItemList() { Id = 3, Price = 300, ExpiryDate = new DateTime(2010, 6, 1) },
|
@@ -30,12 +30,12 @@
|
|
30
30
|
item => item.Id,
|
31
31
|
(trader, item) => new { trader, item })
|
32
32
|
// シーケンスの展開
|
33
|
-
.SelectMany(
|
33
|
+
.SelectMany(traderAndItem => traderAndItem.item, (trader, item) => new {
|
34
|
-
Id =
|
34
|
+
Id = trader.trader.Id,
|
35
|
-
CompanyNm =
|
35
|
+
CompanyNm = trader.trader.CompanyNm,
|
36
|
-
Address =
|
36
|
+
Address = trader.trader.Address,
|
37
|
-
Price =
|
37
|
+
Price = item.Price,
|
38
|
-
ExpireDate =
|
38
|
+
ExpireDate = item.ExpiryDate
|
39
39
|
})
|
40
40
|
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
41
41
|
.ToList();
|
@@ -58,6 +58,8 @@
|
|
58
58
|
public DateTime ExpiryDate { get; set; }
|
59
59
|
}
|
60
60
|
}
|
61
|
+
|
62
|
+
|
61
63
|
```
|
62
64
|
|
63
65
|
出力。
|
1
ToListを追加
answer
CHANGED
@@ -37,7 +37,8 @@
|
|
37
37
|
Price = p.Price,
|
38
38
|
ExpireDate = p.ExpiryDate
|
39
39
|
})
|
40
|
-
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
40
|
+
.Where(o => o.ExpireDate > new DateTime(2010, 1, 1))
|
41
|
+
.ToList();
|
41
42
|
|
42
43
|
foreach (var o in result) {
|
43
44
|
Console.WriteLine($"Id={o.Id}, CompanyNm={o.CompanyNm}, Address={o.Address}, Price={o.Price}, ExpireDate={o.ExpireDate}");
|
@@ -57,7 +58,6 @@
|
|
57
58
|
public DateTime ExpiryDate { get; set; }
|
58
59
|
}
|
59
60
|
}
|
60
|
-
|
61
61
|
```
|
62
62
|
|
63
63
|
出力。
|