假设我有din文件
"Id" : "lot1",
"Fruits" : [
[{ "Id": "fruit1", "Name": "apple"}, { "Id": "fruit2", "Name": "carrot"}]
[{ "Id": "fruit3", "Name": "banana"}]
]
问:如何通过水果名称列表查询Fruits数组?
我试过这样的话:
var fruitNames = new List<string>(){ "apple", "banana" };
var builder = Builders<Lot>.Filter;
var filter = builder.AnyIn(l => l.Fruits.Select(f => f.Name), fruitNames); //TAKE 1
var filter = builder.Where(l => l.Fruits.Select(f => f.Name).Any(f => fruitNames.Contains(f)));//TAKE 2
var filter = builder.AnyIn("Fruits.Name", fruitNames );//TAKE 3
var results = mongoContext.Lots.Find(filter).ToList();
我尝试了这3种方法但没有成功。
答案 0 :(得分:1)
你能试试吗?这是一个小小的黑客,但我很确定它会起作用:
var filter= Builders<Lot>.Filter.ElemMatch(y => y.fruits, x => fruitNames.Contains(x.name));