1

I have a situation where I want to query an accounts table. I want all accounts except accounts where ParentId != null as default.

I have tried the following with the predicate builder, but it does not take effect in the sql query. Any idea how to add the whereClause.And(a => a.ParentId != null); check when using the LinqKit PredicateBuilder?

 var whereClause = PredicateBuilder.New<VAccount>(true);

 whereClause = whereClause.And(a => a.ParentId != null); // Not added to query

 if (dto.OrganisationId != 0)
    whereClause = whereClause.Or(a => a.OrganisationId == dto.OrganisationId);
 
 // Other search inputs added etc..
IceCode
  • 1,466
  • 13
  • 22
  • Maybe because you have used `Or` later? – Svyatoslav Danyliv Jan 12 '23 at 09:23
  • Why are you using `And` & `Or` after each other? I think this blocks out you will probably need nest the `Or` inside the `And` – Roe Jan 12 '23 at 09:51
  • You guys are right about `And` and `Or` after each other is not working. I have changed my approach by filtering all accounts like `accounts.Where(a => a.ProfileId != null)` before running it through the predicatebuildere where clauses. Just consious about that could have a performance hit.. – IceCode Jan 12 '23 at 10:59
  • 1
    Are you working with EF? Then no performance hit. – Svyatoslav Danyliv Jan 12 '23 at 14:52
  • Yes I´m working with EF. Thanks for the info. – IceCode Jan 13 '23 at 07:43

0 Answers0