In addition for my previous question: mongodb c# select specific field.
I'm writing a generic method for selecting a specific field.
the requirements are:
for shorts, im looking for the "select" / dot notation capability. for example:
the wanted method:
T GetFieldValue<T>(string id, string fieldName)
the document:
persons
{
"id": "avi"
"Freinds" : [
{
"Name" : "joni",
"age" : "33"
},
{
"Name" : "daniel",
"age" : "27"
}]
}
The goal is to call the method like this:
string[] myFriends = GetFieldValue<string[]>("avi", "Freinds.Name");
myFriends == ["joni","daniel"]
as far as i know, using projection expression with lambda is no good for items in array, I was thinking more dot notation way.
note: I'm using the new c# driver (2.0)
Thanks A lot.
I don't see good approach with don notation in string, because it has more issues with collections than generic approach:
For example Persion.Friends.Name
Generic methods are more reliable in support and using:
var friends = await GetFieldValue<Person, Friend[]>("avi", x => x.Friends);
var names = friends.Select(x=>x.Name).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With