Skip to content

Commit c224b4f

Browse files
committed
create core/linq component
1 parent 0897d82 commit c224b4f

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

src/app/core/core-routing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { RouterModule, Routes } from '@angular/router';
33
import { OverviewComponent } from './overview/overview.component';
44
import { PropertiesComponent } from './properties/properties.component';
55
import { ExtensionComponent } from './extension/extension.component';
6+
import { LinqComponent } from './linq/linq.component';
67
import { GenericsComponent } from './generics/generics.component';
78
import { AsynchronousComponent } from './asynchronous/asynchronous.component';
89

910
const routes: Routes = [
1011
{ path: 'docs/core/overview', component: OverviewComponent },
1112
{ path: 'docs/core/properties', component: PropertiesComponent },
1213
{ path: 'docs/core/extension', component: ExtensionComponent },
14+
{ path: 'docs/core/linq', component: LinqComponent },
1315
{ path: 'docs/core/generics', component: GenericsComponent },
1416
{ path: 'docs/core/asynchronous', component: AsynchronousComponent },
1517
{ path: 'docs/core', redirectTo: 'docs/core/overview', pathMatch: 'full' }

src/app/core/core.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SharedModule } from '../shared/shared.module';
55
import { OverviewComponent } from './overview/overview.component';
66
import { PropertiesComponent } from './properties/properties.component';
77
import { ExtensionComponent } from './extension/extension.component';
8+
import { LinqComponent } from './linq/linq.component';
89
import { GenericsComponent } from './generics/generics.component';
910
import { AsynchronousComponent } from './asynchronous/asynchronous.component';
1011

@@ -13,6 +14,7 @@ import { AsynchronousComponent } from './asynchronous/asynchronous.component';
1314
OverviewComponent,
1415
PropertiesComponent,
1516
ExtensionComponent,
17+
LinqComponent,
1618
GenericsComponent,
1719
AsynchronousComponent
1820
],

src/app/core/linq/linq.component.css

Whitespace-only changes.
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<div class="container pt-3">
2+
<div class="row">
3+
<div id="sidebar" style="width: 260px;">
4+
<app-sidebar></app-sidebar>
5+
</div>
6+
<div id="content" class="flex-grow-1" style="width: 400px;">
7+
<article>
8+
<h1>Language Integrated Query</h1>
9+
<hr>
10+
<p>
11+
LINQ, or Language Integrated Query, is an integration of query capabilities as expressions of extension methods against data, such as in-memory data, document databases, or SQL databases.
12+
</p>
13+
<p>
14+
Any collection class of type <code>IEnumerable</code> or <code>IQueryable</code> that uses the <code>DevNet\System\Extension\Tweak</code> to support the extension method can take advantage of using LINQ extension methods.
15+
</p>
16+
<p>
17+
The difference between <code>IEnumerable</code> and <code>IQueryable</code> types is that the implementation of the <code>IEnumerable</code> type, like the <code>ArrayList</code>, uses LINQ against in-memory data, while the implementation of the <code>IQueryable</code> type, like the <code>EntitySet</code> repository of DevNet Entity ORM, uses LINQ against SQL database, which means that this one uses <code>IQueryProvider</code> to compile the predicate expressions of the LINQ methods to SQL query syntax.
18+
</p>
19+
<h3>Using LINQ extension methods</h3>
20+
<p>
21+
We use the <code>ArrayList</code> collection as an example to show you how to use the Linq extension methods with the <code>IEnumerable</code> collection and make sure to declare the namespace <code>DevNet\System\Linq</code> to be able to use those extension methods.
22+
</p>
23+
<pre><code class="language-php">&lt;?php
24+
25+
// declaring the Linq namespace to be able to use the extension methods with the ArrayList instance.
26+
use DevNet\System\Linq;
27+
28+
// creating ArrayList instance with generic type argument of Employee class.
29+
$list = new ArrayList(Employee::class);
30+
31+
// assuming we have Employee class with constructor that initializes Id and Name properties.
32+
$list[] = new Employee(11, 'bob');
33+
$list[] = new Employee(12, 'carol');
34+
$list[] = new Employee(13, 'ted');
35+
$list[] = new Employee(14, 'alice');
36+
$list[] = new Employee(15, 'carol');
37+
$list[] = new Employee(16, 'ted');
38+
</code></pre>
39+
<br>
40+
<h5>Picking</h5>
41+
<pre><code class="language-php">$first = $list->first();
42+
$last = $list->last();
43+
print("First employee is &lcub;$first->Name} with Id &lcub;$first->Id}\n");
44+
print("Last employee is &lcub;$last->Name} with Id &lcub;$last->Id}\n");
45+
</code></pre>
46+
<h6>Output:</h6>
47+
<pre><code class="language-shell">First employee is bob with Id 11
48+
Last employee is ted with Id 16
49+
</code></pre>
50+
<br>
51+
<h5>Filtring</h5>
52+
<pre><code class="language-php">print("Employees with Id greater than 12\n");
53+
54+
$employees = $list->where(fn($employee) => $employee->Id > 12);
55+
foreach ($employees as $employee) &lcub;
56+
print("&lcub;$employee->Id} => &lcub;$employee->Name}\n");
57+
}
58+
59+
print("\n");
60+
print("Employees with Name ted\n");
61+
62+
$employees = $list->where(fn($employee) => $employee->Name == 'ted');
63+
foreach ($employees as $employee) &lcub;
64+
print("&lcub;$employee->Id} => &lcub;$employee->Name}\n");
65+
}
66+
</code></pre>
67+
<h6>Output:</h6>
68+
<pre><code class="language-shell">Employees with Id greater than 12
69+
13 => ted
70+
14 => alice
71+
15 => carol
72+
16 => ted
73+
74+
Employees with Name ted
75+
13 => ted
76+
16 => ted
77+
</code></pre>
78+
<br>
79+
<h5>Sorting</h5>
80+
<pre><code class="language-php">print("Ascending order by Name\n");
81+
82+
$employees = $list->orderBy(fn($employee) => $employee->Name);
83+
foreach ($employees as $employee) &lcub;
84+
print("&lcub;$employee->Id} => &lcub;$employee->Name}\n");
85+
}
86+
87+
print("\n");
88+
print("Descending order by Id\n");
89+
90+
$employees = $list->orderByDescending(fn($employee) => $employee->Id);
91+
foreach ($employees as $employee) &lcub;
92+
print("&lcub;$employee->Id} => &lcub;$employee->Name}\n");
93+
}
94+
</code></pre>
95+
<h6>Output:</h6>
96+
<pre><code class="language-shell">Ascending order by Name
97+
14 => alice
98+
11 => bob
99+
12 => carol
100+
15 => carol
101+
13 => ted
102+
16 => ted
103+
104+
Descending order by Id
105+
16 => ted
106+
15 => carol
107+
14 => alice
108+
13 => ted
109+
12 => carol
110+
11 => bob
111+
</code></pre>
112+
<br>
113+
<h5>Paging</h5>
114+
<pre><code class="language-php">// skip first 2 employees then take just 3 from the rest of employees.
115+
$employees = $list->skip(2)->take(3);
116+
117+
foreach ($employees as $employee) &lcub;
118+
print("&lcub;$employee->Id} => &lcub;$employee->Name}\n");
119+
}
120+
</code></pre>
121+
<h6>Output:</h6>
122+
<pre><code class="language-shell">13 => ted
123+
14 => alice
124+
15 => carol
125+
</code></pre>
126+
<br>
127+
<h5>Aggregating</h5>
128+
<pre><code class="language-php">$count = $list->count();
129+
$max = $list->max(fn($employee) => $employee->Id);
130+
$min = $list->min(fn($employee) => $employee->Id);
131+
132+
print("Tolat employess => &lcub;$count}\n");
133+
print("Max employee Id => &lcub;$max}\n");
134+
print("Min employee Id => &lcub;$min}\n");
135+
</code></pre>
136+
<h6>Output:</h6>
137+
<pre><code class="language-shell">Tolat employess => 6
138+
Max employee Id => 16
139+
Min employee Id => 11
140+
</code></pre>
141+
<br>
142+
<h5>Grouping</h5>
143+
<pre><code class="language-php">// create groups of employees share the same name.
144+
$groups = $list->groupBy(fn($employee) => $employee->Name);
145+
146+
foreach ($groups as $group) &lcub;
147+
print("Total employees in &lcub;$group->Key} group => &lcub;$group->count()}\n");
148+
}
149+
</code></pre>
150+
<h6>Output:</h6>
151+
<pre><code class="language-shell">Total employees in bob group => 1
152+
Total employees in carol group => 2
153+
Total employees in ted group => 2
154+
Total employees in alice group => 1
155+
</code></pre>
156+
</article>
157+
<nav class="no-print" aria-label="Page navigation">
158+
<ul class="nav-page">
159+
<li class="nav-page-item">
160+
<a class="nav-page-link" routerLink="/docs/core/extension">
161+
<i class="chevron left"></i> Previous
162+
</a>
163+
</li>
164+
<li class="nav-page-item">
165+
<a class="nav-page-link" routerLink="/docs/core/generics">
166+
Next <i class="chevron right"></i>
167+
</a>
168+
</li>
169+
</ul>
170+
</nav>
171+
</div>
172+
</div>
173+
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { LinqComponent } from './linq.component';
4+
5+
describe('LinqComponent', () => {
6+
let component: LinqComponent;
7+
let fixture: ComponentFixture<LinqComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [ LinqComponent ]
12+
})
13+
.compileComponents();
14+
15+
fixture = TestBed.createComponent(LinqComponent);
16+
component = fixture.componentInstance;
17+
fixture.detectChanges();
18+
});
19+
20+
it('should create', () => {
21+
expect(component).toBeTruthy();
22+
});
23+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import hljs from 'highlight.js/lib/common';
3+
4+
@Component({
5+
selector: 'core-linq',
6+
templateUrl: './linq.component.html',
7+
styleUrls: ['./linq.component.css']
8+
})
9+
export class LinqComponent implements OnInit {
10+
11+
constructor() { }
12+
13+
ngOnInit(): void {
14+
hljs.highlightAll();
15+
}
16+
17+
}

0 commit comments

Comments
 (0)