Skip to content

Commit dce45f3

Browse files
Merge pull request #511 from waqasm78/master
Update query-filter.md
2 parents 3eb51ef + be0cc63 commit dce45f3

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

docs2/pages/documentations/query-filter.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var list = ctx.Posts.ToList();
2121

2222
```
2323

24+
[Try it in EF6](https://dotnetfiddle.net/066xSv) | [Try it in EF Core](https://dotnetfiddle.net/m38JPM)
25+
2426
- You can control the default query to add a default sorting:
2527

2628
{% include template-example.html %}
@@ -37,6 +39,8 @@ var list = ctx.Posts.ToList();
3739

3840
```
3941

42+
[Try it in EF6](https://dotnetfiddle.net/F7mGZM) | [Try it in EF Core](https://dotnetfiddle.net/AjRuQO)
43+
4044
- You can use a predefined filter and enable it only for a specific query:
4145

4246
{% include template-example.html %}
@@ -49,9 +53,10 @@ ctx.Filter<Post>(MyEnum.EnumValue, q => q.Where(x => !x.IsSoftDeleted)).Disable(
4953

5054
// SELECT * FROM Post WHERE IsSoftDeleted = false
5155
var list = ctx.Posts.Filter(MyEnum.EnumValue).ToList();
52-
5356
```
5457

58+
[Try it in EF6](https://dotnetfiddle.net/5bmB2n) | [Try it in EF Core](https://dotnetfiddle.net/1tnpPm)
59+
5560
## EF+ Query Filter Global
5661

5762
Global filter can be used by any context.
@@ -70,9 +75,10 @@ QueryFilterManager.InitilizeGlobalFilter(ctx);
7075

7176
// SELECT * FROM Customer WHERE IsActive = true
7277
var list = ctx.Customers.ToList();
73-
7478
```
7579

80+
[Try it in EF6](https://dotnetfiddle.net/2XfyGS) | [Try it in EF Core](https://dotnetfiddle.net/IFndNf)
81+
7682
***Use entities context constructor to initialize global filter by default.***
7783

7884
## EF+ Query Filter By Instance
@@ -89,9 +95,10 @@ ctx.Filter<Customer>(q => q.Where(x => x.IsActive));
8995

9096
// SELECT * FROM Customer WHERE IsActive = true
9197
var list = ctx.Customers.ToList();
92-
9398
```
9499

100+
[Try it in EF6](https://dotnetfiddle.net/Tyw4Xy) | [Try it in EF Core](https://dotnetfiddle.net/UjIXDH)
101+
95102
***Use entities context constructor to make some filter "global" to all context.***
96103

97104
## EF+ Query Filter By Query
@@ -112,6 +119,8 @@ var list = ctx.Customers.Filter(MyEnum.EnumValue).ToList();
112119

113120
```
114121

122+
[Try it in EF6](https://dotnetfiddle.net/UOS9t5) | [Try it in EF Core](https://dotnetfiddle.net/diPOAn)
123+
115124
## EF+ Query Filter By Inheritance/Interface
116125

117126
Filter can be enabled and disabled by class inheritance and interface.
@@ -136,6 +145,8 @@ var dogs = ctx.Dogs.ToList();
136145

137146
```
138147

148+
[Try it in EF6](https://dotnetfiddle.net/K6nmYU) | [Try it in EF Core](https://dotnetfiddle.net/iX5gWN)
149+
139150
## EF+ Query Filter Enable/Disable
140151

141152
Filters are very flexible, you can enable and disable them at any time and only for a specific inheritance or interface if desired.
@@ -147,7 +158,7 @@ Filters are very flexible, you can enable and disable them at any time and only
147158
var ctx = new EntitiesContext();
148159

149160
// CREATE filter by interface
150-
ctx.Filter<IAnimal>(MyEnum.EnumValue, q => q.Where(x => x.IsDomestic))
161+
ctx.Filter<IAnimal>(MyEnum.EnumValue, q => q.Where(x => x.IsDomestic));
151162

152163
// DISABLE filter only for class inheriting from BaseDog
153164
ctx.Filter(MyEnum.EnumValue).Disable();
@@ -163,6 +174,8 @@ var dogs = ctx.Dogs.ToList();
163174

164175
```
165176

177+
[Try it in EF6](https://dotnetfiddle.net/6aRIYu) | [Try it in EF Core](https://dotnetfiddle.net/JG2gkF)
178+
166179
## EF+ Query Filter AsNoFilter
167180

168181
You can bypass all filters by using AsNoFilter method in a query if a special scenario doesn't require filtering.
@@ -183,6 +196,8 @@ var list = ctx.Customers.AsNoFilter().ToList();
183196

184197
```
185198

199+
[Try it in EF6](https://dotnetfiddle.net/xxM1Tl) | [Try it in EF Core](https://dotnetfiddle.net/El05r4)
200+
186201
## Real Life Scenarios
187202

188203
### Logical Data Partitioning
@@ -207,6 +222,8 @@ var list = ctx.Products.ToList();
207222

208223
```
209224

225+
[Try it in EF6](https://dotnetfiddle.net/IZhSC0) | [Try it in EF Core](https://dotnetfiddle.net/FALmEw)
226+
210227
**Many categories by product**
211228

212229
{% include template-example.html %}
@@ -250,6 +267,8 @@ var list = ctx.Invoices.Where(q => q.Where(x => x.CustomerID = myCustomerID)).To
250267

251268
```
252269

270+
[Try it in EF6](https://dotnetfiddle.net/EyLwE0) | [Try it in EF Core](https://dotnetfiddle.net/uIeV60)
271+
253272
### Object State
254273

255274
Removing inactive or soft deleted records is probably the most common scenario. A soft delete is often useful when related data cannot be deleted. For example, the customer cannot be deleted because related orders cannot be deleted instead, he becomes inactive.
@@ -268,6 +287,8 @@ var list = ctx.Categories.ToList();
268287

269288
```
270289

290+
[Try it in EF6](https://dotnetfiddle.net/4vcAQA) | [Try it in EF Core](https://dotnetfiddle.net/1AcyWB)
291+
271292
### Security Access
272293

273294
Viewing sensible data often requires some permissions. For example, not everyone can see all posts in a forum.
@@ -288,6 +309,8 @@ var list = ctx.Posts.ToList();
288309

289310
```
290311

312+
[Try it in EF6](https://dotnetfiddle.net/3fsHRV) | [Try it in EF Core](https://dotnetfiddle.net/BknS6x)
313+
291314
### Default Ordering
292315

293316
Default ordering can be often useful for base table like category. No matter the query, you probably want to show categories by alphabetic order.
@@ -359,6 +382,8 @@ var list = ctx.SetFiltered<Post>().ToList();
359382

360383
```
361384

385+
[Try it in EF6](https://dotnetfiddle.net/cTEtCX) | [Try it in EF Core](https://dotnetfiddle.net/3203HR)
386+
362387
For this kind of scenario, we recommend using instead: [EntityFramework.DynamicFilters](https://github.com/zzzprojects/EntityFramework.DynamicFilters).
363388
364389
#### Entity Framework Core - Limitations

docs2/pages/documentations/query-future.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var states = futureStates.ToList();
3030

3131
```
3232

33+
[Try it in EF6](https://dotnetfiddle.net/NnXMtb) | [Try it in EF Core](https://dotnetfiddle.net/8gwESn)
3334

3435
## EF+ Query Future
3536

@@ -55,6 +56,8 @@ var states = futureStates.ToList();
5556

5657
```
5758

59+
[Try it in EF6](https://dotnetfiddle.net/NnXMtb) | [Try it in EF Core](https://dotnetfiddle.net/8gwESn)
60+
5861
## EF+ Query FutureValue
5962

6063
Query FutureValue delays the execution of the query returning a result.
@@ -77,6 +80,8 @@ int maxPrice = futureMinPrice.Value;
7780

7881
```
7982

83+
[Try it in EF6](https://dotnetfiddle.net/4K4Fx2) | [Try it in EF Core](https://dotnetfiddle.net/gNCsOR)
84+
8085
## EF+ Query FutureValue Deferred
8186

8287
Immediate resolution methods like **Count()** and **FirstOrDefault()** cannot use future methods since it executes the query immediately.
@@ -91,6 +96,7 @@ var count = ctx.Customers.Count();
9196
var count = ctx.Customers.Future().Count();
9297

9398
```
99+
[Try it in EF6](https://dotnetfiddle.net/lLkiUc) | [Try it in EF Core](https://dotnetfiddle.net/62LQVi)
94100

95101
**EF+ Query Deferred** has been created to resolve this issue. The resolution is now deferred instead of being immediate which lets you use FutureValue and get the expected result.
96102

@@ -101,7 +107,7 @@ var count = ctx.Customers.Future().Count();
101107
var ctx = new EntitiesContext();
102108

103109
// GET the first active customer and the number of active customers
104-
var futureFirstCustomer = ctx.Customer.DeferredFirstOrDefault().FutureValue();
110+
var futureFirstCustomer = ctx.Customers.DeferredFirstOrDefault().FutureValue();
105111
var futureCustomerCount = ctx.Customers.DeferredCount().FutureValue();
106112

107113
// TRIGGER all pending queries
@@ -112,6 +118,8 @@ var count = futureCustomerCount.Value;
112118

113119
```
114120

121+
[Try it in EF6](https://dotnetfiddle.net/V2ifb0) | [Try it in EF Core](https://dotnetfiddle.net/BI16rq)
122+
115123
## Real Life Scenarios
116124

117125
- Multi tables information scenario: Client and all related information (order, invoice, etc.) must be loaded.
@@ -151,6 +159,7 @@ var postCount = futurePostCount.Value;
151159

152160
```
153161

162+
[Try it in EF6](https://dotnetfiddle.net/oA24FP) | [Try it in EF Core](https://dotnetfiddle.net/PyyUsy)
154163
## Behind the code
155164

156165
- All queries from a context using query future are added to a batch list.

docs2/pages/documentations/query-include-filter.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var list = ctx.Orders.IncludeFilter(x => x.Items.Where(y => !y.IsSoftDeleted)
1919
.ToList();
2020

2121
```
22+
[Try it in EF6](https://dotnetfiddle.net/Duyw5p) | [Try it in EF Core](https://dotnetfiddle.net/1unMtl)
2223

2324
## EF+ Query IncludeFilter
2425

@@ -36,6 +37,7 @@ var ctx = new EntitiesContext();
3637
var blogs = ctx.Blogs.IncludeFilter(x => x.Posts.Where(y => !y.IsSoftDeleted)).ToList();
3738

3839
```
40+
[Try it in EF6](https://dotnetfiddle.net/10sM7d) | [Try it in EF Core](https://dotnetfiddle.net/1unMtl)
3941

4042
### Load multiple levels
4143

@@ -53,6 +55,7 @@ var blogs = ctx.Blogs.IncludeFilter(x => x.Posts.Where(y => !y.IsSoftDeleted))
5355
.ToList();
5456

5557
```
58+
[Try it in EF6](https://dotnetfiddle.net/gFGxt6) | [Try it in EF Core](https://dotnetfiddle.net/SK934m)
5659

5760
## Real Life Scenarios
5861

@@ -71,6 +74,7 @@ var posts= ctx.Posts.IncludeFilter(x => x.Comments
7174
.ToList();
7275

7376
```
77+
[Try it in EF6](https://dotnetfiddle.net/WpLt3A) | [Try it in EF Core](https://dotnetfiddle.net/IMdizK)
7478

7579
- Security Access (Include available related entities)
7680

@@ -87,6 +91,7 @@ var posts= ctx.Posts.IncludeFilter(x => x.Comments.Where(y => y.RoleID >= myRole
8791
.ToList();
8892

8993
```
94+
[Try it in EF6](https://dotnetfiddle.net/8hXy4V) | [Try it in EF Core](https://dotnetfiddle.net/RKvLJU)
9095

9196
- Soft Deleted Records (Include active related entities)
9297

@@ -101,6 +106,7 @@ var posts= ctx.Posts.IncludeFilter(x => x.Comments.Where(y => !y.IsSoftDeleted))
101106
.ToList();
102107

103108
```
109+
[Try it in EF6](https://dotnetfiddle.net/sGsBlh) | [Try it in EF Core](https://dotnetfiddle.net/qXHvYM)
104110

105111
## Behind the code
106112

docs2/pages/documentations/query-include-optimized.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var ctx = new EntitiesContext();
6060
var orders = ctx.Orders.IncludeOptimized(x => x.Items);
6161

6262
```
63+
[Try it in EF6](https://dotnetfiddle.net/d8P4FS)
6364

6465
## EF+ Query IncludeOptimized Filter
6566

@@ -74,6 +75,7 @@ var ctx = new EntitiesContext();
7475
var orders = ctx.Orders.IncludeOptimized(x => x.Items.Where(y => y.IsActive));
7576

7677
```
78+
[Try it in EF6](https://dotnetfiddle.net/uFBqTO)
7779

7880
### AllowQueryBatch
7981

@@ -147,4 +149,4 @@ EF+ Query IncludeOptimized drastically decreases the amount of data transferred
147149

148150
Need help getting started? [[email protected]](mailto:[email protected])
149151

150-
We welcome all comments, ideas and suggestions to improve our library.
152+
We welcome all comments, ideas and suggestions to improve our library.

0 commit comments

Comments
 (0)