WPF ObjectCollection到List

时间:2015-03-19 03:22:24

标签: c# wpf mvvm

我想知道如何正确地从我的MongoDB数据库绑定我的数据。我正在使用MVVM模式,但是当我打算将数据库中的数据组合到gridview时,我不确定viewModel上有什么问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Morza.Model;
using System.Collections.ObjectModel;
using System.Web;
using System.IO;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using System.Windows;
using Morza.Helper;
using Morza.View;

namespace Morza.ModelView
{
    internal class BillMetaDataViewModel
    {
        private ObservableCollection<BillMetaData> _MyDataList;

        public BillMetaDataViewModel()
        {
            Database dbobj = new Database();
            MongoDatabase dtbase = dbobj.getDatabase;
            var collection = dtbase.GetCollection<BillMetaData>("BillMetaData");

            bool isKey = true;
            string attName = "Name";
            int attType = 1;
            bool isRequired = true;
            int attLoc = 1;
            int attLength = 30;
            int isDecimal = 1;
            int attAlignment = 1;

            BillMetaData _obj = new BillMetaData(isKey, attName, attType, isRequired, attLoc, attLength, isDecimal, attAlignment);
            BillMetaData _obj1 = new BillMetaData(isKey, attName, attType, isRequired, attLoc, attLength, isDecimal, attAlignment);
            BillMetaData _obj2 = new BillMetaData(isKey, attName, attType, isRequired, attLoc, attLength, isDecimal, attAlignment);
            collection.Insert(_obj);
            collection.Insert(_obj1);
            collection.Insert(_obj2);
            var query = Query<BillMetaData>.EQ(e => e.attName, "Name");
            var entity = collection.FindAll();
            _MyDataList = entity.ToList<BillMetaData>();
        }

        public ObservableCollection<BillMetaData> FileObjectCollection
        {
            get { return _MyDataList; }
        }
    }
}

ToList声明它无法将常规列表转换为集合。我该怎么用呢?

1 个答案:

答案 0 :(得分:1)

试试这个:

_MyDataList = new ObservableCollection<BillMetaData>(entity.ToList());