回答編集履歴

1

【追伸】追加

2016/09/23 10:32

投稿

退会済みユーザー
test CHANGED
@@ -11,3 +11,161 @@
11
11
  (data["詳細項目_No"] >= 1 && data["詳細項目_No"] <= n)
12
12
 
13
13
  ```
14
+
15
+
16
+
17
+ 【追伸】
18
+
19
+
20
+
21
+ コメントに書きましたが、検証に使ったサンプルをアップしておきます。
22
+
23
+
24
+
25
+ ```
26
+
27
+ using System;
28
+
29
+ using System.Collections.Generic;
30
+
31
+ using System.Linq;
32
+
33
+ using System.Text;
34
+
35
+ using System.Data;
36
+
37
+
38
+
39
+ namespace ConsoleAppLinq2
40
+
41
+ {
42
+
43
+ class Program
44
+
45
+ {
46
+
47
+ // データソース用の DataTable を作成
48
+
49
+ protected DataTable CreateDataSource()
50
+
51
+ {
52
+
53
+ DataTable dt = new DataTable();
54
+
55
+ DataRow dr;
56
+
57
+
58
+
59
+ dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
60
+
61
+ dt.Columns.Add(new DataColumn("Name", typeof(string)));
62
+
63
+ dt.Columns.Add(new DataColumn("Type", typeof(string)));
64
+
65
+ dt.Columns.Add(new DataColumn("Price", typeof(Int32)));
66
+
67
+ dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));
68
+
69
+ dt.Columns.Add(new DataColumn("Amount", typeof(Int32)));
70
+
71
+ dt.Columns.Add(new DataColumn("CategoryID", typeof(Int32)));
72
+
73
+ dt.Columns.Add(new DataColumn("Note", typeof(string)));
74
+
75
+ dt.Columns.Add(new DataColumn("Discontinued", typeof(bool)));
76
+
77
+ dt.Columns.Add(new DataColumn("DateTime", typeof(DateTime)));
78
+
79
+
80
+
81
+ for (int i = 0; i < 25; i++)
82
+
83
+ {
84
+
85
+ dr = dt.NewRow();
86
+
87
+ dr["ID"] = i;
88
+
89
+ dr["Name"] = "Product Name_" + i.ToString();
90
+
91
+ dr["Type"] = "Product Type " + (100 - i).ToString();
92
+
93
+ dr["Price"] = 123000 * (i + 1);
94
+
95
+ dr["Qty"] = (i + 1) * 20;
96
+
97
+ dr["Amount"] = 123000 * (i + 1) * (i + 1);
98
+
99
+ dr["CategoryID"] = 100 - i;
100
+
101
+ dr["Note"] = "Note_" + i.ToString();
102
+
103
+ dr["Discontinued"] = (i % 2 == 0) ? true : false;
104
+
105
+ dr["DateTime"] = DateTime.Now.AddDays(i);
106
+
107
+ dt.Rows.Add(dr);
108
+
109
+ }
110
+
111
+ return dt;
112
+
113
+ }
114
+
115
+
116
+
117
+ static void Main(string[] args)
118
+
119
+ {
120
+
121
+ Program prg = new Program();
122
+
123
+ DataTable getDt = prg.CreateDataSource();
124
+
125
+
126
+
127
+ int n = 5;
128
+
129
+ var query = from data in getDt.AsEnumerable()
130
+
131
+ where ((int)data["ID"] >=1 && (int)data["ID"] <= n)
132
+
133
+ select data;
134
+
135
+
136
+
137
+ foreach (DataRow row in query)
138
+
139
+ {
140
+
141
+ Console.WriteLine("ID: {0}, Name: {1}, Type: {2}, Price: {3}",
142
+
143
+ row["ID"], row["Name"], row["Type"], row["Price"]);
144
+
145
+ }
146
+
147
+
148
+
149
+ /*
150
+
151
+ 結果は:
152
+
153
+ ID: 1, Name: Product Name_1, Type: Product Type 99, Price: 246000
154
+
155
+ ID: 2, Name: Product Name_2, Type: Product Type 98, Price: 369000
156
+
157
+ ID: 3, Name: Product Name_3, Type: Product Type 97, Price: 492000
158
+
159
+ ID: 4, Name: Product Name_4, Type: Product Type 96, Price: 615000
160
+
161
+ ID: 5, Name: Product Name_5, Type: Product Type 95, Price: 738000
162
+
163
+ */
164
+
165
+ }
166
+
167
+ }
168
+
169
+ }
170
+
171
+ ```