1

I try to get all data from collection into MongoDB server using C# driver. The idea is connect to the server and get all collection than insert into list of class.

List<WatchTblCls> wts;
List<UserCls> users;
List<SymboleCls> syms;
public WatchTbl()
{
 InitializeComponent();
 wts = new List<WatchTblCls>();
 users = new List<UserCls>();
 syms = new List<SymboleCls>();
}
public async void getAllData()
{
 client = new MongoClient("mongodb://servername:27017");
 database = client.GetDatabase("WatchTblDB");
 collectionWatchtbl = database.GetCollection<WatchTbl>("Watchtbl");
 collectionUser = database.GetCollection<UserCls>("Users");
 collectionSymbole = database.GetCollection<SymboleCls>("Users");
 var filter = new BsonDocument();

 using (var cursor = await collectionWatchtbl.FindAsync(filter))
 {
   while (await cursor.MoveNextAsync())
   {
     var batch = cursor.Current;
     foreach (var document in batch)
     {
       wts.Add(new WatchTblCls(document["_id"], document["userId"], document["wid"], document["name"], document["Symboles"]));
      }
    }
  }
  }

I get this error under

wts.Add(new WatchTblCls(document["_id"], document["userId"], document["wid"], document["name"], document["Symboles"]));

Cannot apply indexing with [] to an expression of type 'WatchTbl'

7
  • Looks like you're trying to read a full collection into a different projection class here. Am I right? Commented Sep 10, 2016 at 14:13
  • yes, because when I try to show the collection directly from the server it takes time (too long) so I need to insert it into list 1st than ad the list into datagridview Commented Sep 10, 2016 at 14:15
  • Can't you do some kind of pagination where you don't need to get the full collection altogether? Instead you get first 50, then in next page you get next 50? If you collection is too big then it would take a lot of memory when you store it into a local List. Commented Sep 10, 2016 at 14:19
  • maybe in next step I wil do that, but now I need to show it Commented Sep 10, 2016 at 14:21
  • 1
    Even if you insert into a list first, you're iterating all the data. If you have a lot of data and the internet connection is slow, taking it into the list will not help you anyway as you will go through all the data anyway. :) Try the answer and lemme know whether it works for you or not. But pagination is your best answer now. Commented Sep 10, 2016 at 14:40

1 Answer 1

2

I don't understand the reason behind using WatchTbl and WatchTblCls both together. Is WatchTblCls a model for the entity WatchTbl here? Im not sure.

In any case. If you go for aggregation and want to convert WatchTbl collection to WatchTblCls list, your desired solution might look like the following. I don't know the defiitions of the classes so I'm assuming:

    var client = new MongoClient("mongodb://servername:27017");
    var database = client.GetDatabase("WatchTblDB");
    var collectionWatchtbl = database.GetCollection<WatchTbl>("Watchtbl");
    var collectionUser = database.GetCollection<UserCls>("Users");
    var collectionSymbole = database.GetCollection<SymboleCls>("Users");

    var list = collectionWatchtbl.AsQueryable().Select(x => new WatchTblCls() {
        id = x.id,
        userId = x.userId,
        .....
    });

If you can use the same WatchTbl class and still want to load the full collection to a local List (which is definitely not a good idea):

    List<WatchTbl> list = await collectionWatchtbl.Find(x => true).ToListAsync();
Sign up to request clarification or add additional context in comments.

1 Comment

WatchTbl is a form and WatchTblCls is class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.