Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

save and query mongodb collection as dynamic ExpandoObject

QUESTION:
I'm saving a dynamic object in my database but I would also like to retrieve it as a dynamic object. How can this be done? I tried it like so:

public dynamic GetItemById(ObjectId id)
{
    dynamic result = Db.GetCollection<dynamic>("Items").Find(x => x.Id == id).FirstOrDefaultAsync().Result;
    return result;
}


ANSWER:
You can use the string-based syntax, since the expression doesn't offer any advantages with dynamic anyway:
  
var cursor = db.GetCollection<dynamic>("foo").
                Find(Builders<dynamic>.Filter.Eq("_id", someId));

or

var cursor = db.GetCollection<ExpandoObject>("foo").
                Find(Builders<ExpandoObject>.Filter.Eq("_id", someId));
Comment

PREVIOUS NEXT
Code Example
Csharp :: ASP.NET C# Catch all exceptions in a class 
Csharp :: c# how to disable a event 
Csharp :: dictionary plus generic class c# 
Csharp :: how to combine constructors in c# 
Csharp :: how to input message ox in c# 
Csharp :: c# Unit Test IDbContextFactory 
Csharp :: cannot convert from group method to threadstart C# 
Csharp :: pyqt send message to another instance 
Csharp :: serach a keyword in whole database 
Csharp :: mvc input number rounding 
Csharp :: Running C# Example 
Csharp :: get number of sundays in a month c# 
Csharp :: redsymbol.net exit traps 
Csharp :: how to show enum name list from input in swagger c# 
Csharp :: c# registrykey is null 
Csharp :: declare a delegate 
Csharp :: ask int text c# 
Csharp :: c# ile ürün çekme - htmlagilitypack 
Csharp :: save current dir shell 
Csharp :: backcolor app winform C3 
Csharp :: WPF combobox filter as you type 
Csharp :: LINQ return list of unique values with counts 
Csharp :: return a list of list from yaml via C# 
Csharp :: C# Floating Point Literals 
Csharp :: c# use readonly array as method default 
Csharp :: How to truncate a decimal without rounding 
Csharp :: unity download image from online 
Csharp :: class combining 
Csharp :: delay seconds in unity 
Csharp :: wpf user parent controller datacontext 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =