首页 > 解决方案 > LiteDB - 通过 List.contains 查找数据对象

问题描述

简化版。我有两节课:

Public Class mSystem

    Public Property ID as ObjectID
    Public Property Name as string

End Class

Public Class mEmulator

    Public Property ID as ObjectID
    Public Property Name as string
    <BsonRef("mSystems")>
    Public Property AssociatedSystems as New List(Of mSystem)

End Class

Public Class Main

    Public Sub EmaultorsLinkedToSystem      

        dim SelectedSystem as mSystem = db.Collections.mSystems.Find(Function(x) x.Name = "Sony Playstation").FirstOrDefault

        test = db.Collections.mEmulators.Include(Function(x) x.AssociatedSystems).Find(Function(y) y.AssociatedSystems.Contains(SelectedSystem)).ToList

    End sub 

End Class

现在我知道一个 mEmulator 数据对象在其 List(of mSystem) 中有“Sony Playstation”。但是,test返回 null。这个怎么找不到?我已经尝试了一些排列,但无法让它发挥作用。有任何想法吗?

标签: listwhere-clauselitedb

解决方案


Include方法用于解析对其他集合的引用,并且您没有使用BsonRefwith AssociatedSystems(至少在您提供的这个示例中没有)。在您的示例中,mSystemin的实例AssociatedSystems没有存储在单独的集合中,而是作为 emulators 集合中的嵌入文档数组。

尝试删除Include呼叫,它应该可以正常工作。


推荐阅读