0

I am trying to match my model code with the same structure as my json file. The json file has jsonArray which works fine as ReportList if accountlist (another array inside array items) did not exist.

Now I am trying to make it work with account list. So I created MyListType which matches the structure of accountlist in the json file.

Unfortunately, I am getting an error saying accountlist in json file does not match any field or property of Report class. (which I think should be matching with MyListType) Please help me to resolve this error. Thanks in advance.

The Model code

public class Report
{
 public Object _id { get; set; }
 public string username { get; set; }
 public List<MyListType> accountlist{get; set;}
}
public class MyListType
{
 public string address1 {get; set;}
 public string address2 {get; set;}
}

Partial Controller Code

MongoCollection<Report> collection = Context.Database.GetCollection<Report>("report");
var ReportList = new List<Report>();
//Right below I get: Errors out here Element 'accountlist' does not match any field or //property of class MyProject.Models.Report.
foreach (Report doc in collection.FindAll()) 
{
 ReportList.Add(doc);
}

report collection

{
   "username" : "doodle",
   "accountlist" : [
                      {"address1": "abc st", "address2": "efg st"},
                      {"address1": "hijk st", "address2": "mno st"}
                   ]
  }

  {
   "username" : "doodle2",
   "accountlist" : [
                      {"address1": "abc st", "address2": "efg st"},
                      {"address1": "hijk st", "address2": "mno st"}
                   ]
  }
2
  • so after much digging I did find similar question stackoverflow.com/questions/5135576/… From this post I learned that I need to change my FindAll() to FindAllAs<Report>() in the foreach loop in my code. But I am still getting the error though. I know I am getting close. Commented Jun 6, 2014 at 21:58
  • Changing FindAll() to FindAllAs<Report>() did not make any difference. Commented Jun 9, 2014 at 15:04

2 Answers 2

0

You might need to register this these types in the ClassMap

  if (!BsonClassMap.IsClassMapRegistered(typeof(Report))) BsonClassMap.RegisterClassMap<Report>(cm => { cm.AutoMap(); cm.SetIgnoreExtraElements(true); });

  if (!BsonClassMap.IsClassMapRegistered(typeof(MyListType))) BsonClassMap.RegisterClassMap<MyListType>(cm => { cm.AutoMap(); cm.SetIgnoreExtraElements(true); });

  var reports = _collection.FindAllAs<Report>().ToList();
Sign up to request clarification or add additional context in comments.

Comments

0

After looking at my code more, I found out that the problem was that I used different name for my field "accountslist" vs the one in the collection accountlist. The field names in the class that represents collection needs to match exactly.

Comments

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.