1 |
#Region "Microsoft.VisualBasic::de99f877192f15b393c35879ea83067e, Microsoft.VisualBasic.Core\Language\Linq\Linq.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
#End Region
|
83 |
|
84 |
Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic
|
85 |
Imports Microsoft.VisualBasic.Language.LinqAPIHelpers
|
86 |
Imports Microsoft.VisualBasic.Linq
|
87 |
Imports Microsoft.VisualBasic.Parallel.Linq
|
88 |
Imports Microsoft.VisualBasic.Serialization
|
89 |
Imports Microsoft.VisualBasic.Serialization.JSON
|
90 |
|
91 |
Namespace Language
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
Public NotInheritable Class LinqAPI
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
Private Sub New()
|
103 |
End Sub
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
Public Shared Function MakeList(Of T)() As ListHelper(Of T)
|
111 |
Return New ListHelper(Of T)
|
112 |
End Function
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
Public Shared Function Exec(Of T)() As ExecHelper(Of T)
|
120 |
Return New ExecHelper(Of T)
|
121 |
End Function
|
122 |
|
123 |
Public Shared Function DefaultFirst(Of T)(Optional [default] As T = Nothing) As FirstOrDefaultHelper(Of T)
|
124 |
Return New FirstOrDefaultHelper(Of T)([default])
|
125 |
End Function
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
<typeparam name="T">Is the type of linq source</typeparam>
|
131 |
<typeparam name="V">Is the type of value output</typeparam>
|
132 |
<param name="source"></param>
|
133 |
<returns></returns>
|
134 |
Public Shared Function Exec(Of T, V)(source As IEnumerable(Of T)) As ToArrayHelper(Of T, V)
|
135 |
Return New ToArrayHelper(Of T, V)(source)
|
136 |
End Function
|
137 |
|
138 |
Public Shared Function BuildHash(Of T, V, [In])(keys As Func(Of [In], T), values As Func(Of [In], V)) As BuildHashHelper(Of T, V, [In])
|
139 |
Return New BuildHashHelper(Of T, V, [In])(keys, values)
|
140 |
End Function
|
141 |
|
142 |
Public Shared Function BuildHash(Of T, [In])(keys As Func(Of [In], T)) As BuildHashHelper(Of T, [In], [In])
|
143 |
Return New BuildHashHelper(Of T, [In], [In])(keys, Function(x) x)
|
144 |
End Function
|
145 |
|
146 |
Public Shared Function BuildHash(Of T As INamedValue)() As BuildHashHelper(Of String, T, T)
|
147 |
Return New BuildHashHelper(Of String, T, T)(Function(x) x.Key, Function(x) x)
|
148 |
End Function
|
149 |
|
150 |
Public Shared Function Takes(Of T)(n As Integer) As TakeHelper(Of T)
|
151 |
Return New TakeHelper(Of T)(n)
|
152 |
End Function
|
153 |
|
154 |
Public Shared Function LQuery(Of T, out)(task As Func(Of T, out), Optional partTokens As Integer = 20000) As LQueryHelper(Of T, out)
|
155 |
Return New LQueryHelper(Of T, out) With {
|
156 |
.task = task,
|
157 |
.partTokens = partTokens
|
158 |
}
|
159 |
End Function
|
160 |
|
161 |
Public Shared Function IsEquals(Of T)(Optional c% = 0) As CountHelper(Of T)
|
162 |
Return New CountHelper(Of T) With {
|
163 |
.count = c
|
164 |
}
|
165 |
End Function
|
166 |
End Class
|
167 |
|
168 |
Namespace LinqAPIHelpers
|
169 |
|
170 |
Public Structure CountHelper(Of T)
|
171 |
|
172 |
Public count%
|
173 |
|
174 |
|
175 |
判断序列计数是否相等
|
176 |
|
177 |
<param name="h"></param>
|
178 |
<param name="source"></param>
|
179 |
<returns></returns>
|
180 |
Public Shared Operator <=(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
|
181 |
Return source.Count = h.count
|
182 |
End Operator
|
183 |
|
184 |
Public Shared Operator >=(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
|
185 |
Throw New NotSupportedException
|
186 |
End Operator
|
187 |
|
188 |
Public Shared Operator =(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
|
189 |
Return h.count = source.Count
|
190 |
End Operator
|
191 |
|
192 |
Public Shared Operator <>(h As CountHelper(Of T), source As IEnumerable(Of T)) As Boolean
|
193 |
Return Not h = source
|
194 |
End Operator
|
195 |
End Structure
|
196 |
|
197 |
Public Structure LQueryHelper(Of T, out)
|
198 |
|
199 |
Dim task As Func(Of T, out)
|
200 |
Dim partTokens As Integer
|
201 |
|
202 |
Public Overloads Shared Operator <=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of T)) As out()
|
203 |
Return LQuerySchedule.LQuery(source, helper.task, helper.partTokens).ToArray
|
204 |
End Operator
|
205 |
|
206 |
Public Overloads Shared Operator >=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of T)) As out()
|
207 |
Throw New NotSupportedException
|
208 |
End Operator
|
209 |
|
210 |
Public Overloads Shared Operator <=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of IEnumerable(Of T))) As out()
|
211 |
Return helper <= source.IteratesALL
|
212 |
End Operator
|
213 |
|
214 |
Public Overloads Shared Operator >=(helper As LQueryHelper(Of T, out), source As IEnumerable(Of IEnumerable(Of T))) As out()
|
215 |
Throw New NotSupportedException
|
216 |
End Operator
|
217 |
End Structure
|
218 |
|
219 |
Public Structure TakeHelper(Of T)
|
220 |
|
221 |
Public ReadOnly Property n As Integer
|
222 |
|
223 |
Sub New(n As Integer)
|
224 |
Me.n = n
|
225 |
End Sub
|
226 |
|
227 |
Public Overrides Function ToString() As String
|
228 |
Return Me.GetJson
|
229 |
End Function
|
230 |
|
231 |
Public Overloads Shared Operator <=(num As TakeHelper(Of T), source As IEnumerable(Of T)) As IEnumerable(Of T)
|
232 |
Return source.Take(num.n)
|
233 |
End Operator
|
234 |
|
235 |
Public Overloads Shared Operator >=(num As TakeHelper(Of T), source As IEnumerable(Of T)) As IEnumerable(Of T)
|
236 |
Throw New NotSupportedException
|
237 |
End Operator
|
238 |
End Structure
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
Public Structure BuildHashHelper(Of T, V, [In])
|
268 |
|
269 |
ReadOnly __keys As Func(Of [In], T)
|
270 |
ReadOnly __values As Func(Of [In], V)
|
271 |
|
272 |
Sub New(k As Func(Of [In], T), v As Func(Of [In], V))
|
273 |
__keys = k
|
274 |
__values = v
|
275 |
End Sub
|
276 |
|
277 |
Public Shared Operator <=(cls As BuildHashHelper(Of T, V, [In]), linq As IEnumerable(Of [In])) As Dictionary(Of T, V)
|
278 |
Return linq.ToDictionary(cls.__keys, cls.__values)
|
279 |
End Operator
|
280 |
|
281 |
Public Shared Operator >=(cls As BuildHashHelper(Of T, V, [In]), linq As IEnumerable(Of [In])) As Dictionary(Of T, V)
|
282 |
Throw New NotSupportedException
|
283 |
End Operator
|
284 |
End Structure
|
285 |
|
286 |
Public Structure ListHelper(Of T)
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
<param name="linq">The collection whose elements are copied to the new list.</param>
|
294 |
<returns></returns>
|
295 |
Public Shared Operator <=(cls As ListHelper(Of T), linq As IEnumerable(Of T)) As List(Of T)
|
296 |
Return linq.AsList
|
297 |
End Operator
|
298 |
|
299 |
Public Shared Operator >=(cls As ListHelper(Of T), linq As IEnumerable(Of T)) As List(Of T)
|
300 |
Throw New NotSupportedException
|
301 |
End Operator
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
<param name="linq">The collection whose elements are copied to the new list.</param>
|
309 |
<returns></returns>
|
310 |
Public Shared Operator <=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As List(Of T)
|
311 |
Return linq.Unlist
|
312 |
End Operator
|
313 |
|
314 |
Public Shared Operator >=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As List(Of T)
|
315 |
Throw New NotSupportedException
|
316 |
End Operator
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
<param name="linq">The collection whose elements are copied to the new list.</param>
|
324 |
<returns></returns>
|
325 |
Public Shared Operator <=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of IEnumerable(Of T)))) As List(Of T)
|
326 |
Return linq.IteratesALL.Unlist
|
327 |
End Operator
|
328 |
|
329 |
Public Shared Operator >=(cls As ListHelper(Of T), linq As IEnumerable(Of IEnumerable(Of IEnumerable(Of T)))) As List(Of T)
|
330 |
Throw New NotSupportedException
|
331 |
End Operator
|
332 |
End Structure
|
333 |
|
334 |
|
335 |
Execute a linq expression. Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
|
336 |
|
337 |
|
338 |
Public Structure ExecHelper(Of T)
|
339 |
|
340 |
|
341 |
Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
|
342 |
|
343 |
<param name="cls"></param>
|
344 |
<param name="linq">
|
345 |
An <see cref="System.Collections.Generic.IEnumerable(Of T)"/> to create an array from.
|
346 |
</param>
|
347 |
|
348 |
Public Shared Operator <=(cls As ExecHelper(Of T), linq As IEnumerable(Of T)) As T()
|
349 |
Return linq.ToArray
|
350 |
End Operator
|
351 |
|
352 |
Public Shared Operator >=(cls As ExecHelper(Of T), linq As IEnumerable(Of T)) As T()
|
353 |
Throw New NotSupportedException
|
354 |
End Operator
|
355 |
|
356 |
|
357 |
Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
|
358 |
|
359 |
<param name="cls"></param>
|
360 |
<param name="linq">
|
361 |
An <see cref="System.Collections.Generic.IEnumerable(Of T)"/> to create an array from.
|
362 |
</param>
|
363 |
|
364 |
Public Shared Operator <=(cls As ExecHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As T()
|
365 |
Return linq.ToVector
|
366 |
End Operator
|
367 |
|
368 |
Public Shared Operator >=(cls As ExecHelper(Of T), linq As IEnumerable(Of IEnumerable(Of T))) As T()
|
369 |
Throw New NotSupportedException
|
370 |
End Operator
|
371 |
End Structure
|
372 |
|
373 |
Public Structure FirstOrDefaultHelper(Of T)
|
374 |
|
375 |
ReadOnly __default As T
|
376 |
|
377 |
Sub New(__default As T)
|
378 |
Me.__default = __default
|
379 |
End Sub
|
380 |
|
381 |
|
382 |
Exec <see cref="IEnumerable(Of T)"/>.DefaultFirst extension method.
|
383 |
|
384 |
<param name="cls"></param>
|
385 |
<param name="linq"></param>
|
386 |
<returns></returns>
|
387 |
Public Shared Operator <=(cls As FirstOrDefaultHelper(Of T), linq As IEnumerable(Of T)) As T
|
388 |
Return linq.DefaultFirst(cls.__default)
|
389 |
End Operator
|
390 |
|
391 |
Public Shared Operator >=(cls As FirstOrDefaultHelper(Of T), linq As IEnumerable(Of T)) As T
|
392 |
Throw New NotSupportedException
|
393 |
End Operator
|
394 |
End Structure
|
395 |
|
396 |
|
397 |
Execute a linq expression. Creates an array from a <see cref="System.Collections.Generic.IEnumerable(Of T)"/>.
|
398 |
|
399 |
|
400 |
Public Structure ToArrayHelper(Of T, V)
|
401 |
|
402 |
Private __source As IEnumerable(Of T)
|
403 |
|
404 |
Sub New(source As IEnumerable(Of T))
|
405 |
__source = source
|
406 |
End Sub
|
407 |
|
408 |
|
409 |
Exec ToArray
|
410 |
|
411 |
<param name="cls"></param>
|
412 |
<param name="linq"></param>
|
413 |
<returns></returns>
|
414 |
Public Shared Operator <=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, V)) As V()
|
415 |
Return (From x As T In cls.__source Select linq(x)).ToArray
|
416 |
End Operator
|
417 |
|
418 |
Public Shared Operator >=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, V)) As V()
|
419 |
Throw New NotSupportedException
|
420 |
End Operator
|
421 |
|
422 |
|
423 |
Exec ToArray
|
424 |
|
425 |
<param name="cls"></param>
|
426 |
<param name="linq"></param>
|
427 |
<returns></returns>
|
428 |
Public Shared Operator <=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, Integer, V)) As V()
|
429 |
Return (From x As SeqValue(Of T)
|
430 |
In cls.__source.SeqIterator
|
431 |
Select linq(x.value, x.i)).ToArray
|
432 |
End Operator
|
433 |
|
434 |
Public Shared Operator >=(cls As ToArrayHelper(Of T, V), linq As Func(Of T, Integer, V)) As V()
|
435 |
Throw New NotSupportedException
|
436 |
End Operator
|
437 |
End Structure
|
438 |
End Namespace
|
439 |
End Namespace
|