Skip to content

Commit ac69193

Browse files
authored
Update ef6-query-db-set-filter.md
1 parent 13c74ab commit ac69193

File tree

1 file changed

+8
-136
lines changed

1 file changed

+8
-136
lines changed

docs2/pages/ef-docs/documentations/query-db-set-filter/ef6-query-db-set-filter.md

Lines changed: 8 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -25,139 +25,11 @@ To make both features live together, method in Query DbSetFilter has been rename
2525
|Filter |DbSetFilter|
2626
|QueryFilterManager |QueryDbSetFilterManager|
2727

28-
## EF+ Query Filter Global
29-
30-
Global filter can be used by any context.
31-
32-
Global filter is normally preferred in most scenarios over instance filter since the filter code is centralized in one method over being spread in multiple methods.
33-
34-
{% include template-example.html %}
35-
```csharp
36-
37-
// using Z.EntityFramework.Plus; // Don't forget to include this.
38-
QueryDbSetFilterManager.Filter<Customer>(q => q.Where(x => x.IsActive));
39-
40-
var ctx = new EntitiesContext();
41-
// TIP: You can also add this line in EntitiesContext constructor instead
42-
QueryDbSetFilterManager.InitilizeGlobalFilter(ctx);
43-
44-
// SELECT * FROM Customer WHERE IsActive = true
45-
var list = ctx.Customers.ToList();
46-
47-
```
48-
{% include component-try-it.html href='https://dotnetfiddle.net/HijoZX' %}
49-
50-
***Use entities context constructor to initialize global filter by default.***
51-
52-
## EF+ Query Filter By Instance
53-
54-
Instance filter applies filters to the current context only. The filtering logic is added once the context is created.
55-
56-
{% include template-example.html %}
57-
```csharp
58-
59-
// using Z.EntityFramework.Plus; // Don't forget to include this.
60-
var ctx = new EntitiesContext();
61-
62-
ctx.DbSetFilter<Customer>(q => q.Where(x => x.IsActive));
63-
64-
// SELECT * FROM Customer WHERE IsActive = true
65-
var list = ctx.Customers.ToList();
66-
67-
```
68-
{% include component-try-it.html href='https://dotnetfiddle.net/3Xz39f' %}
69-
70-
***Use entities context constructor to make some filter "global" to all context.***
71-
72-
## EF+ Query Filter By Query
73-
74-
Query filter applies filters to specific queries only. The filtering logic is added globally or by instance but in a disabled state and then it is enabled by these specific queries.
75-
76-
{% include template-example.html %}
77-
```csharp
78-
79-
// using Z.EntityFramework.Plus; // Don't forget to include this.
80-
var ctx = new EntitiesContext();
81-
82-
// CREATE a disabled filter
83-
ctx.DbSetFilter<Customer>(MyEnum.EnumValue, q => q.Where(x => x.IsActive), false);
84-
85-
// SELECT * FROM Customer
86-
var list = ctx.Customers.DbSetFilter(MyEnum.EnumValue).ToList();
87-
88-
```
89-
{% include component-try-it.html href='https://dotnetfiddle.net/XmtVWN' %}
90-
91-
## EF+ Query Filter By Inheritance/Interface
92-
93-
Filter can be enabled and disabled by class inheritance and interface.
94-
95-
{% include template-example.html %}
96-
```csharp
97-
98-
// using Z.EntityFramework.Plus; // Don't forget to include this.
99-
var ctx = new EntitiesContext();
100-
101-
// CREATE filter by inheritance
102-
ctx.DbSetFilter<BaseDog>(q => q.Where(x => !x.IsDangerous));
103-
104-
// CREATE filter by interface
105-
ctx.DbSetFilter<IAnimal>(q => q.Where(x => x.IsDomestic));
106-
107-
// SELECT * FROM Cat WHERE IsDomestic = 1
108-
var cats = ctx.Cats.ToList();
109-
110-
// SELECT * FROM Dog WHERE IsDomestic = 1 AND IsDangerous = 0
111-
var dogs = ctx.Dogs.ToList();
112-
113-
```
114-
{% include component-try-it.html href='https://dotnetfiddle.net/flFnBf' %}
115-
116-
## EF+ Query Filter Enable/Disable
117-
118-
Filters are very flexible, you can enable and disable them at any time and only for a specific inheritance or interface if desired.
119-
120-
{% include template-example.html %}
121-
```csharp
122-
123-
// using Z.EntityFramework.Plus; // Don't forget to include this.
124-
var ctx = new EntitiesContext();
125-
126-
// CREATE filter by interface
127-
ctx.DbSetFilter<IAnimal>(MyEnum.EnumValue, q => q.Where(x => x.IsDomestic))
128-
129-
// DISABLE filter
130-
ctx.DbSetFilter(MyEnum.EnumValue).Disable();
131-
132-
// SELECT * FROM Dog
133-
var dogs = ctx.Dogs.ToList();
134-
135-
// ENABLE filter
136-
ctx.DbSetFilter(MyEnum.EnumValue).Enable();
137-
138-
// SELECT * FROM Dog WHERE IsDomestic = true
139-
var dogs = ctx.Dogs.ToList();
140-
141-
```
142-
{% include component-try-it.html href='https://dotnetfiddle.net/girbYB' %}
143-
144-
## EF+ Query Filter AsNoFilter
145-
146-
You can bypass all filters by using AsNoFilter method in a query if a special scenario doesn't require filtering.
147-
148-
{% include template-example.html %}
149-
```csharp
150-
151-
// using Z.EntityFramework.Plus; // Don't forget to include this.
152-
var ctx = new EntitiesContext();
153-
154-
this.DbSetFilter<Customer>(q => q.Where(x => x.IsActive));
155-
156-
// SELECT * FROM Customer WHERE IsActive = true
157-
var list = ctx.Customers.ToList();
158-
159-
// SELECT * FROM Customer
160-
var list = ctx.Customers.AsNoDbSetFilter().ToList();
161-
162-
```
163-
{% include component-try-it.html href='https://dotnetfiddle.net/ZIA1kt' %}
28+
## Options
29+
30+
- [Global](options/ef6-query-db-set-filter-global.md)
31+
- [By Instance](options/ef6-query-db-set-filter-by-instance.md)
32+
- [By Query](options/ef6-query-db-set-filter-by-query.md)
33+
- [By Inheritance/Interface](options/ef6-query-db-set-filter-by-inheritance-interface.md)
34+
- [By Enable/Disable](options/ef6-query-db-set-filter-by-enable-disable.md)
35+
- [By AsNoFilter](options/ef6-query-db-set-filter-by-as-no-filter.md)

0 commit comments

Comments
 (0)