Error thrown when trying to convert from MongoDB BsonArray to IEnumerable<Users> a collection of a POCO instances

ぐ巨炮叔叔 提交于 2019-12-24 11:53:36

问题


All:

Here is the information about my development environment:

MongoDB 3.0.0

MongoDB C# Driver Version 1.7.0.4714

Microsoft Visual Studio Professional 2013

.NET Framework 4.0

Here is one of the POCO Classes that is used in our project:

    public class AppUsers
        {

            public Object Id { get; set; }
            public int UserID { get; set; }
            public int CompanyID { get; set; }
            public string Username { get; set; }
            public string Password { get; set; }
            public int RoleID { get; set; }
        public DateTime LoginTime { get; set; }
        public DateTime LogoutTime { get; set; }
    }

Here is a quasi-stored procedure JavaScript function that queries the MongoDB Database's AppUsers collection:

function (userIdArg 
              , companyIdArg
                 , searchTermArg
                    , referenceRoleIdOfInterestArg
                     , startRowOfInterestArg
                       , displayedRowsQuantityArg
                        , sortColumnArg
                        , isAscendingArg){



    var usrColl = [];                  


        usrColl = db.getCollection('AppUsersCollection').find(   {     "CompanyID" : companyIdArg }).sort( { sortColumnArg : isAscendingArg } ).skip(startRowOfInterestArg).limit(displayedRowsQuantityArg).toArray();

returns usrColl;
}

Here is the Excerpt from client code that will invoke the aforementioned JavaScript code:

            var sysJs = DBConnection.database.GetCollection("system.js");

            sysJs.Remove(Query.EQ("_id", "getUsers"));

            var code = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Hos/quasiStoredProceduresJS/getUsers.js"));


            var codeDocument = new BsonDocument("value", new    BsonJavaScript(code));

            codeDocument.Add(new BsonElement("_id", "getUsers"));

            sysJs.Insert(codeDocument);

 BsonValue getUsers = DBConnection.database.Eval("getUsers");


            BsonValue bv3 =  DBConnection.database.Eval(getUsers.AsBsonJavaScript.Code, null
                                                                                            ,
loggedInUser.CompanyID
                                                                                                     ,
searchTermArg
                                                                                                      ,
ApplicationConstants.DriverRole
                                                                                                   ,
startRowOfInterest
                                                                                                    ,
displayedRowsQuantity
                                                                                                     ,
sortColumn
                                                                                                     ,
isAscending);


            IEnumerable<Users> usersOfInterestList =     bv3.AsBsonArray.AsQueryable();

The aforementioned line is where I'm having trouble which gives me the following error:

Error   90  Cannot implicitly convert type     'System.Linq.IQueryable<MongoDB.Bson.BsonValue>' to     'System.Collections.Generic.IEnumerable<Users>'. An explicit conversion     exists (are you missing a cast?) 

How do I implement the code so that I can easily convert BSONArray containing Users to an IEnumerable collection?

Thanks in Advance.


回答1:


I used the BsonSerializer to Deserialize the JSON as in the following line of code:

IEnumerable<AppUsers> usersOfInterestList =     BsonSerializer.Deserialize<List<AppUsers>>(bv3.ToJson());


来源:https://stackoverflow.com/questions/35554495/error-thrown-when-trying-to-convert-from-mongodb-bsonarray-to-ienumerableusers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!