Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

postgresql - Entity Framework Core with multiple Where clause joined by OR operator

I build a NetCore 3.1 application who use Postgres as database engine, and I have an entity SomeEntity who contains:

  • Id : int
  • Name: text
  • Tags: text[]

I try to build a filter on the attribute Tags. It must only select rows where the Tags array contains one or more elements of an unknown length array argument.

Let's use an example !

We have this SomeEntity with the data set bellow:

Id Name Tags
1 n1 array ("tag1", "tag2", "tag3")
2 n2 array ("tag2", "tag3")
3 n2 array ("tag3")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As proposed by Guru Stron, I've give a try to LINQKit, that I didn't know.

The code I searched for hours was waiting for me right here!
IQueryable<Product> SearchProducts (params string[] keywords)
{
  var predicate = PredicateBuilder.New<Product>();

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
  return dataContext.Products.Where (predicate);
}

Thanks a lot! You saved my day.

But it's really strange that we can't do it without an external library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...