Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

How get the ToList() of objects from MongoDB to C#

public class RootObject1
    {
        
        public Dictionary<string, string> nodeHierarchy { get; set; }
        public Dictionary<string, string> Parentnode { get; set; }
        public string wsId { get; set; }
        public DateTime dateFrom { get; set; }
        public DateTime dateTo { get; set; }
        public bool IsLeafNode { get; set; }
        public decimal weightage { get; set; }
}
/** 
* this is what i have in mongodb
* Paste one or more documents here
*/
{
    "Parentnode": {
        "LEVEL1": "Civil and Structural",
        "LEVEL2": "Cluster 1",
        "LEVEL3": "Workshop & Maintenance Building"
    },
    "nodeHierarchy": {
        "LEVEL1": "Civil and Structural",
        "LEVEL2": "Cluster 1",
        "LEVEL3": "Workshop & Maintenance Building",
        "LEVEL4": "Excavation"
    },
    "weightage": "20",
    "dateFrom": "2019-01-01",
    "dateTo": "2019-12-31",
    "IsLeafNode": true,
    "wsId": "PROJECT2"
}

List<Models.RootObject1> bsonPlanData2 = collection.AsQueryable().Where(e1 => e1.wsId == WSId).Select(e => new { e.nodeHierarchy, e.Parentnode,e.wsId,e.dateFrom,e.dateTo,e.IsLeafNode,e.weightage }).ToList();

List<Models.RootObject1> bsonPlanData2 = collection.AsQueryable().Where(e1 => e1.wsId == WSId).Select(e => new { e.nodeHierarchy, e.Parentnode,e.wsId,e.dateFrom,e.dateTo,e.IsLeafNode,e.weightage }).ToList();

List<Models.RootObject1> bsonPlanData2 = collection.AsQueryable().Where(e1 => e1.wsId == WSId).Select(e => { e.nodeHierarchy, e.Parentnode,e.wsId,e.dateFrom,e.dateTo,e.IsLeafNode,e.weightage }).ToList();

List<Models.RootObject1> bsonPlanData2 = collection.AsQueryable().Where(e1 => e1.wsId == WSId).Select(e => new RootObject1() { e.nodeHierarchy, e.Parentnode,e.wsId,e.dateFrom,e.dateTo,e.IsLeafNode,e.weightage }).ToList();

above 4 ways are there, where i tried to get the data from mongo and put it in my c# object.
though there is one newtonsoft deserializer is there , but somebody told me that my 4 call also deserializes, so i do not want to use extra conversion by usng newtornsoft desrilizer.

and above code shows me that u have to implemnt ienumerator in my class. so pls tel me is there any othe way i could do this with out implimenting ienumertoe in my class.

As you can read here:
https://mongodb-documentation.readthedocs.io/en/latest/ecosystem/tutorial/use-linq-queries-with-csharp-driver.html

Select must be the last operation in the Linq chain. You must first get the result of the select, then you can get a list from it using ToList from the result.