簡體   English   中英

C# MongoDb Driver Filter 使用集合中的嵌套屬性

[英]C# MongoDb Driver Filter using nested property from 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();

這是返回一個空過濾器並且不匹配任何內容? 我應該如何將表達式傳遞給 In Filter ? 有任何想法嗎 ?

有很多方法,例如:

        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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM