f-miyu / Plugin.CloudFirestore

MIT License
121 stars 44 forks source link

subcollection with mapping little example #29

Open ghost opened 4 years ago

ghost commented 4 years ago

Can you please provide a little code snippet in which you show how to get a document which includes a subcollection and that maps this whole document with subcollection to a related .net class? Thanks.

ElishaMisoi commented 4 years ago

Have you tried using .ToObject(); ?

var example = await CrossCloudFirestore.Current.Instance.GetDocument("path/path").GetDocumentAsync(); var your_NetObject = example.ToObject<Models.YourObject>();

If your document that has a subcollection in the database marches your .net class then you'll be able to get that document as a .net class.

The easiest way of making sure your .net class marches your document in the database is this:

get your document:

var example = await CrossCloudFirestore.Current.Instance.GetDocument("path/path").GetDocumentAsync();

Console.WriteLine(example.Data);

This will give you the document in a Json format if the document exists. Copy that Json and go to the site: http://json2csharp.com/

It will generate a .NET class for you. If it does not march your class then you'll have to use the generated class or think of a way of uploading your document to march your class.

After that, then you can easily do:

var your_NetObject = example.ToObject<Models.YourObject>();

ghost commented 4 years ago

I have a subcollection in my document, but its not in the .Data

ElishaMisoi commented 4 years ago

Do you get a Json Object when you log .Data?