簡體   English   中英

Linq查詢哪里包含列表中的列表

[英]Linq query where contains List in a List

我試圖查詢MongoDB以獲得前5000條記錄,這導致以下錯誤。 我正在使用C#LINQ驅動程序,並且TermMonitorIds是BsonArray。

{“無法確定表達式的序列化信息:x.ToString()。”}

public IList<SocialRecord> GetManyBetweenDatesLimited(List<string> termMonitorIds, string[] sources, DateTime fr, DateTime to)
        {
            IList<SocialRecord> entities = new List<SocialRecord>();

            try
            {
                entities =
                    (from e in this.collection.AsQueryable<SocialRecord>()
                     where (e.TermMonitorIds.Any(x => termMonitorIds.Contains(x.ToString()))) && (sources.Contains(e.SocialType))
                                                             && (e.DateCreated.Date >= fr.Date) && (e.DateCreated.Date <= to.Date)
                     select e)
                    .Take(5000)
                    .ToList();
            }
            catch (Exception ex)
            {
                Log.Error("Error Message", ex);
            }

            return entities;
        }

我嘗試將List更改為BsonArray,如下所示:

BsonArray bArray = new BsonArray();
            foreach (var term in termMonitorIds )
            {
                bArray.Add(term.ToBson());
            }

仍然會出現如下錯誤消息:

“ /”應用程序中的服務器錯誤。

不能將String值寫入BSON文檔的根目錄。

說明:執行當前Web請求期間發生未處理的異常。 請查看堆棧跟蹤,以獲取有關錯誤及其在代碼中起源的更多信息。

異常詳細信息:System.InvalidOperationException:無法將字符串值寫入BSON文檔的根級別。

LINQ提供程序不支持ToString因為它不知道如何將其轉換為MongoDB表達式。

我建議你更新termMonitorIds以匹配返回預期的數據類型e.TermMonitorIdsList<int> / List<Guid>避免了任何形式的轉換的需要(通常更有效的是沒有它)。

暫無
暫無

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

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