I am trying to build a LINQ query to query against a large SQL table (7M+ entries) of Documents.
Each document has many DocumentFields :
My goal is to apply successive filters (from 0 to ~10 filters) on the value field of DocumentField:
Here is an example of the filters I want to apply:
[
{fieldId: 32, value: "CET20533"},
{fieldId: 16, value: "882341"},
{fieldId: 12, value: "101746"}
]
What I want is to retrieve every document in my database that matches all of the filters. For the previous example, I want all documents that have a value of CET20533 for the field with the Id "32", the value 882341 for the field with the Id 16, and so on.
I had a first approach :
List<MyFilter> filters = ... // Json deserialization
db.Documents.Where(document =>
filters.All(filter =>
document.DocumentFields.Any(documentField =>
documentField.Id == filter.Id
&& documentField.Value == filter.Value)));
This approach doesn't work : my filters List isn't a primitive type, and therefore cannot be used in a LINQ query.
I had a second approach, which didn't throw an error at me, but only applied 1 filter :
var result = db.Documents.Select(d => d);
foreach (var filter in filters) {
var id = filter.Id;
var value = filter.Value;
result = result.Where(document => document.DocumentFields.Any(documentField =>
documentField.Id == id
&& documentField.Value == value));
}
The problem with this approach is, I believe, some sort of a concurrency problem. I applied a simple pause Thread.Sleep(2000) in each iteration of the foreach to test, and it seems to work.
Questions :
- How to remove the pause and still not have concurrency problems ?
- Is there a better way to build my query ?
EDIT :
For more clarity, here is an actual example of a document that matches the previous filters example :

