【发布时间】:2016-05-10 05:16:31
【问题描述】:
考虑以下结构
public class Parent
{
public ObjectId Id { get; set; }
public IEnumerable<Child> Children { get; set; }
}
public class Child
{
public string Value { get; set; }
}
我想找到其子值是数组超集的所有父对象,即
var parents = new List<Parent>();
var values = new[] { "A", "B", "C" };
parents.Where(x => !values.Except(x.Children.Select(y => y.Value)).Any());
或
{ "Children.Value": { $all: ["A", "B", "C"] } }
我想以键入的方式进行,但谓词翻译器不支持 Enumerable.Select 所以这不起作用:
Builders<Parent>.Filter.All(x => x.Children.Select(y => y.Value), values);
我目前正在使用此解决方法:
var filters = values.Select(x => Builders<Parent>.Filter.Where(y => y.Children.Any(z => z.Value == x)));
Builders<Parent>.Filter.And(filters);
有没有更好的方法不使用魔法字段名称字符串?
【问题讨论】:
-
你有没有想过这个问题?我需要做类似的...
标签: c# arrays mongodb mongodb-query mongodb-.net-driver