c# MongoDb Driver Filter使用来自collection的嵌套属性



我有以下类结构:

public class QuoteRequestInfo
{
. . . 
public LocationInfo LocationInfo {get;set;}
. . .
}

LocationInfo有一个停止集合,它总是包含2个停止:

public class LocationInfo
{
. . . 
public IEnumerable<Stop> Stops{get;set};
. . .
}

Stop具有以下属性:

public class Stop
{
. . . 
public string StateName {get;set};
. . .
}

我试图通过过滤多个状态名称来搜索QuoteRquestInfos集合,如下所示:

var states = new List<string> {"VA"}
var filter =  Builders<QuoteRequestInfo>.Filter.In(r => r.LocationInfo.Stops.First().StateCode, states)
var quotes = await collection.Find(filter).ToListAsync();

返回一个空过滤器,不匹配任何东西?我应该如何在表达式传递到过滤器?有什么想法吗?

有很多方法,例如:

var states = new List<string> { "VA" };
var quotes = coll.AsQueryable()
.Select(c =>
new {
Previous = new QuoteRequestInfo { LocationInfo = c.LocationInfo }, // this partocular line saves the previous document
StoredFirst = c.LocationInfo.Stops.First().StateName }) // save a flag about first stop since call it in the same construction with `Where` is not supported via typed way
.Where(f => states.Contains(f.StoredFirst))
.Select(c => c.Previous)
.ToList();

最新更新