0

I want to add extra "where" clause into the following expression.

public Task<IEnumerable<MyClass>> Find(Expression<Func<MyClass, bool>> predicate)
 {
   return _documentDbProvider.Find(predicate);
 }

such as .where(t=>t.tenantId == tenant.id)

so my db provider gets two "where" clauses, thanks

M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
aurimas
  • 67
  • 10
  • You can compose new expression using and operator or extend yours _documentDbProvider.Find method to accept: params Expression>[] predicates. Second approach seems to be much easier, which one do you prefer? – Vitaliy Kalinin Dec 04 '16 at 10:59
  • You want to use `Find()` and select all IEnumerable using where clause? – M. Wiśnicki Dec 04 '16 at 11:01
  • I would prefer 1st approach, and after googling I seem to not find any examples. – aurimas Dec 04 '16 at 11:02
  • @mww yes, find uses predicate in db provider and adds where clause. ` return _client.CreateDocumentQuery(collection.DocumentsLink) .Where(predicate) .AsEnumerable();` so I want to be tenant aware. that only can be selected for this tenant. – aurimas Dec 04 '16 at 11:05
  • Can show how You call now your `Find()` method. How look now your predicate? – M. Wiśnicki Dec 04 '16 at 11:14
  • There are a lot of them actually, but this decision will definitely made yours code less readable and less maintainable. Here is a link: http://stackoverflow.com/questions/457316/combining-two-expressions-expressionfunct-bool. – Vitaliy Kalinin Dec 04 '16 at 11:19
  • that is in service ` public Task> GetDepotsAsync() { return _repository.Find(x=>x.DepotName == depotName); }` – aurimas Dec 04 '16 at 11:20
  • What about `_repository.Find(x=>x.DepotName == depotName && x.tenantId == tenant.id );` ? – M. Wiśnicki Dec 04 '16 at 11:23
  • so in nutshell, in services I populate depot class properties, in repository I populate base class properties for the depot. also repo can get / find only depots that belongs to that tenant. I dont want to manipulate low level tenant interactions in services. – aurimas Dec 04 '16 at 11:24
  • I want to move tenant info one level down. – aurimas Dec 04 '16 at 11:26

1 Answers1

1

Looks like you are seeking for a way to combine an existing predicate with another one using And. You can use some expression predicate builder helper class - like Universal Predicate Builder or my own PredicateUtils from Establish a link between two lists in linq to entities where clause post and similar. It allows you to use something like this:

return _documentDbProvider.Find(predicate.And(t => t.tenantId == tenant.id));
Community
  • 1
  • 1
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343