The Array intersection is not allowed, but you can write your query as follows:
var colors = session.Query<Color>()
.Where(c =>
c.colorArray.Any(x => x == "r") &&
c.colorArray.Any(x => x == "e") &&
c.colorArray.Any(x => x == "d"));
Please note that in order for this to work, you need to use string
s, not char
s. The colorArray
property in your Color
class needs to be defined as:
public string[] colorArray { get; set; }
The reason is that if you use char[]
, then the query will check for the numeric values (ASCII codes) rather then the string values (ex: 'r'
will be interpreted by the query engine as 114
, 'e'
to 101
and 'd'
to 100
).
Now, to take this to the next step and make the query condition dynamic, with respect to the userInput
string array:
var userInput = new[] {"r", "e", "d"};
var colors = session.Query<Color>();
// dynamically add a WHERE clause for each letter in the array
foreach (var letter in userInput)
{
var currentLetter = letter;
colors = colors.Where(c => c.colorArray.Any(x => x == currentLetter));
}
// display the results
foreach (var color in colors)
Console.WriteLine(color);
This should produce the results you expect: the documents that have "r"
, "e"
and "d"
in their colorArray.