public static List <T> GetCollectionAsList <T>(DBFilterCreator newfilter = null) { FilterDefinition <T> filter; if (newfilter == null) { filter = FilterDefinition <T> .Empty; } else { filter = newfilter.GenerateFilter(); } return(GetCollection <T>().FindSync(filter).ToList()); }
public static void UpdateToCollection <T>(T item) { // var filterElements = new List<FilterElement>(); var filter = new DBFilterCreator(); var updateElements = new Dictionary <string, object>(); var props = item.GetType().GetProperties(); foreach (var propertyInfo in props) { var propValue = propertyInfo.GetValue(item); if (propValue != null) { object[] attrs = propertyInfo.GetCustomAttributes(true); if (attrs.Length > 0) { foreach (object attr in attrs) { KeyAttribute authAttr = attr as KeyAttribute; if (authAttr != null) { // Key Attribute filter.Elements.Add(new FilterElement(propertyInfo.Name, propValue.ToString())); } } } else { bool addToDictionary = true; if (propValue is DateTime) { if ((DateTime)propValue == DateTime.MinValue) { addToDictionary = false; } } if (propValue is IEnumerable <object> ) { if (((IEnumerable <object>)propValue).Count() == 0) { addToDictionary = false; } } if (addToDictionary) { updateElements.Add(propertyInfo.Name, propValue); } } } } // var filter = new DBFilterCreator(filterElements); var updateFilter = new BsonDocument(updateElements); GetCollection <T>().FindOneAndUpdate(filter.GenerateFilter(), new BsonDocument("$set", updateFilter)); }