首页 > 解决方案 > 将 IMongoCollection 转换为 ObservableCollection

问题描述

无法转换IMongoCollection<MatchDocument>ObservableCollection<MatchDocument>

MongoClient client = new MongoClient();
var DB = client.GetDatabase("MTR");
var Collection = DB.GetCollection<MatchDocument>("MATCHES");
App.Profiles = new ObservableCollection<MatchDocument>(Collection);

标签: c#mongodbxamarin.formstype-conversionobservablecollection

解决方案


您可以使用Collection.Find<MatchDocument>(query).ToListAsync()

    MongoClient client = new MongoClient();
    var DB = client.GetDatabase("MTR");
    var Collection = DB.GetCollection<MatchDocument>("MATCHES");

    Task<IList<MatchDocument>> doc = await Collection.Find<MatchDocument>(query).ToListAsync();

    ObservableCollection<MatchDocument> Profiles = new ObservableCollection<MatchDocument>(doc.Result);

参考:无法从 'MongoDB.Driver.IMongoCollection<>' 转换为 'System.Collections.Generic.IEnumerable<>'


推荐阅读