Aniket Narvankar

Aniket Narvankar

  • 559
  • 2.1k
  • 608.1k

Mongodb Like qyery in linq C#

Jan 11 2021 6:32 AM
In my project mongodb is used and linq is used to connect to mongodb.
 
This is the filter query created
 
builder.Where(x => x.OldBatchDesc.Contains(input.oldBatchDescription));
 
Input is model object and oldBatchDescription is property name. OldBatchDesc is column name.
 
In current scenario suppose user enters value ab in textbox this value is set to input.oldBatchDescription, and as per above query it will check in OldBatchDesc column which row will contain ab. For example if there are 5 rows in OldBatchDesc xyz, xyabz, def, defab, abcab, then it will return xyabz, defab, abcab as these contains ab.
 
Now the requirement is user will enter comma separated values in a textbox for example like ab,xy,ef, and I want to check whether OldBatchDesc column row will contain this values ab or xy or ef entered by user.
 
So in this scenario output will be xyz,xyabz,def,defab,abcab.
 
How to build a mongodb filter query for this using C#?
 
What I have done is created a string array and split it on comma
 
string[] desc = input.oldBatchDescription(',')
 
But how should I pass this array to filter query or is there any way to pass this comma separated values to filter to get desired output.
 
builder.Where(x => x.OldBatchDesc.Contains(input.oldBatchDescription));
 
Kindly guide me on this.

Answers (2)