Comments (6)
Hello. My project allows creating global filters on entities using simple boolean expressions.
What you are describing would not apply to my project but can be accomplished with Entity Framework out of the box. I think something like this (in C#, my language of preference) is what you are trying to do?
var mylist = context.Set<BlogEntry>() as IQueryable<BlogEntry>;
if (condition1)
mylist = mylist.Where(b => b.IntValue == 1);
if (condition2)
mylist = mylist.Where(b => b.IntValue == 2);
if (condition3)
mylist = mylist.Where(b => b.ChildCollection.Any(c => c.type == 3));
var result = mylist.ToList();
Basically, you can just keep appending more .Where() clauses to your IQueryable. EF will not execute it until you do the ToList() at the end and the resulting sql query will contain all the conditions you have added to your query.
Hope that helps.
from entityframework.dynamicfilters.
Hello !
The problem is on condition3 .
The way you construct this query would mean : " Select all the Articles that have on "childCollection" at least one child with c.type=3.
If you can read carefully my first post , this is not what I want.
I want : " For all articles in Mylist , Include only those childs from childsCollection that have c.type=3.
This is not supported from Entity Framework out of the box , because if I will translate my request , will be an Include() with filter that EF doesn't support out of the box.
For what I ask you if your product can do anything.
So , is there any solution ?
Thank you !
from entityframework.dynamicfilters.
Sorry, I misunderstood your question and didn't realize you were asking about filtering the child collection.
You can do that with this project. There are examples of doing this in the example project in the source. Look for how a filter is created on BlogEntries. That is a child collection on Accounts. Filters apply to all queries against an entity no matter how they are done - directly against the DbSet or via Include().
If you need to conditionally change the filter, you can do that by either disabling it or changing the parameter values. There are examples of both in the example project and on the main page.
Sorry, I can't give more specifics atm. I'm literally walking out the door for vacation.
from entityframework.dynamicfilters.
Hello !
I have tried , but didn't find something similar on the examples.
I want to repeat that this query is constructed step by step , and if a condition is true another part of filter is applied.
So for Include childs for example the condition 3 , 4 and 5 have dependencies to each other.
Please when you have time , write some code on how can I do this ?
Thank you , in advance.
from entityframework.dynamicfilters.
Please give me some help !
Thank you !
from entityframework.dynamicfilters.
Here is an example how you can accomplish this. I've tried to match it as closely to your VB/pseudo code as possible. I'm not a VB guy so you'll have to convert this from C#.
Models:
public class Article
{
[Key]
public int id { get; set; }
public int vl1 { get; set; }
public ICollection<F1Child> F1Children { get; set; }
public ICollection<F2Child> F2Children { get; set; }
}
public class F1Child
{
[Key]
public int id { get; set; }
public int quantity { get; set; }
public int value5 { get; set; }
}
public class F2Child
{
[Key]
public int id { get; set; }
public int type { get; set; }
}
Filters:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Filter("Condition3Filter", (F1Child c, int quantity) => c.quantity > quantity, () => 0);
modelBuilder.Filter("Condition4Filter", (F1Child c, int value5) => c.value5 == value5, () => 0);
modelBuilder.Filter("Condition5Filter", (F2Child c, int type) => c.type == type, () => 0);
}
Run your query with condition checks and get results:
using (var context = new TestContext())
{
var mylist = context.Set<Article>()
.Include(a => a.F1Children)
.Include(a => a.F2Children) as IQueryable<Article>;
if (condition1)
mylist = mylist.Where(a => a.vl1 == 1);
if (condition2)
mylist = mylist.Where(a => a.id >= 5);
if (condition3)
context.SetFilterScopedParameterValue("Condition3Filter", "quantity", 3);
else
context.DisableFilter("Condition3Filter");
if (condition4)
context.SetFilterScopedParameterValue("Condition4Filter", "value5", 0);
else
context.DisableFilter("Condition4Filter");
if (condition5)
context.SetFilterScopedParameterValue("Condition5Filter", "type", 3);
else
context.DisableFilter("Condition5Filter");
var results = mylist.ToList();
}
I have defined 3 global filters on the F!Child and F2Child entities. These will be enabled by default so you would need to disable them if they should not be used. I am also setting the values using SetFilterScopedParameterValue because I'm assuming those values may vary. 2 of the filters are on F1Child and can be used together or independently.
As I said, the filters are enabled by default. There is currently no way to disable them by default, but you could define the filters something like this:
modelBuilder.Filter("Condition3Filter", (F1Child c, int quantity) => (quantity >= 0) && (c.quantity > quantity), () => -1);
Here, the default value I am setting for "quantity" is -1 so it will effectively return all records (and thus disable the filter) unless "quantity" is set via a call to SetFilterScopedParameterValue() or SetFilterGlobalParameterValue().
Remember that these filters will apply globally to all queries that involve F1Child and F2Child entities. It may be worthwhile for me to add an option to the filter creation that allows the filters to be disabled by default so that you can selectively opt-in to filters that should not be used globally. But until then, you can accomplish that as I have shown above.
from entityframework.dynamicfilters.
Related Issues (20)
- [Oracle EF 6.2] System.MissingMethodException HOT 1
- Conditional filter not working with EntityFramework6.Npgsql 3.2.1.1 HOT 6
- Support for EF 6.3 and netstandard HOT 7
- Context.Set<T>().Find(keyValues) And Use Context.Filter<Interface>() after, HOT 3
- EntityCommandCompilationException when using .Include() on class with subclass HOT 2
- Entity Framework Dynamic Filters not working in dot net core HOT 3
- Another way to initialize filters besides calling modelBuilder.Filter? HOT 1
- Enum-of-byte is unnecessarily cast to int HOT 6
- IsFilterEnabled returns true for non-existent filter HOT 1
- Cannot use a DBContext property in a condition for a dynamic filter. Value dosen't update after initial execution. HOT 1
- Problem creating Filter HOT 2
- Compatibility issue with bulk operations (Z.EntityFramework.Extensions) HOT 6
- Why filters should come last in the model creation? HOT 11
- Filter with claim HOT 3
- Compile error with package reference HOT 2
- Concurrency error when PreventDisabledFilterConditions is called HOT 4
- Issue with dynamic filter applied on eager loading with many-to-many relationship entities using MySQL
- Issue occur when dynamic filter is applied on eager loading with many-to-many relationship entities for MySQL HOT 8
- Filter decoder HOT 1
- ConcurrentDictionary is breaking my Oracle 11g unit tests HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from entityframework.dynamicfilters.