How to apply global filter to Entity Framework?

I have a table in my model called Customers with an IsActive field. Whenever I run a query for Clients, only active clients should be retrieved. I can include a filter in every request, but it doesn’t look very good. I would like to be able to override the Customers property on the Object Context lever, but I'm not sure if this is possible. Any help would be greatly appreciated! Thanks

+4
source share
4 answers

Despite the late reply, I will put it here so that it can help others.

You can also set a condition for your entity in the edmx file. Select your object and click on "Display Details" and create a new condition.

enter image description here

+5
source

Perhaps you could declare a new property and use it:

public partial class MyEntities { public ObjectQuery<User> ActiveCustomers { get { return Customers.Where(c => c.IsActive); } } } 
+3
source

I do not know why this is a problem for you. You can put one request inside some function:

 IEnumerable<Customers> GetActiveCustomers() { var activeCustomers = from cust in db.Customers where cust.IsActive == true select cust; return activeCustomers; } 

And call him every time you want. You can even place active clients in some kind of private list or even better ObservableCollection. Then you can request your result again:

  var myCustomers = from cust in GetActiveCustomers() where cust.CustomerName == "John" select cust; 

and what is he.

0
source

All you have to do is get all the clients, whether they are active or not, and then use

 foreach(Customer c in Results) { if(c.IsActive) listofactivecustomers.Add(c); } 
-5
source

Source: https://habr.com/ru/post/1306728/


All Articles