/// <summary> /// This method converts a document expression to a value for matching /// </summary> /// <param name="document"></param> /// <param name="valueExpression"></param> /// <returns></returns> public static string GetBsonElementNameFromDocument(TDocumentObject document, Expression <Func <TDocumentObject, TDocumentKeyType> > valueExpression) { // Localize the member expression MemberExpression memberExpression = (MemberExpression)valueExpression.Body; // Make sure we have a member expression if (memberExpression != null) { // Localize the property information PropertyInfo property = (memberExpression.Member as PropertyInfo); // Make sure we have property information and set the value if (property != null) { // Localize our BSON element BsonElementAttribute bsonElement = (BsonElementAttribute)property.GetCustomAttributes(typeof(BsonElementAttribute), true) .FirstOrDefault(); // Check to see if we have a BSON element if (bsonElement != null) { return(bsonElement.ElementName); } } } // We're done, return the default value return(null); }
/// <summary> /// Gets the property depending on the BSON property name /// </summary> /// <returns> /// The PropertyInfo object if the property under the given name exists, /// NULL otherwise /// </returns> public static PropertyInfo GetPropertyFromBSONName(this Type type, string bsonPropertyName) { PropertyInfo[] properties = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance); return(properties.Where(p => { BsonElementAttribute bsonElementAttribute = p.GetCustomAttribute <BsonElementAttribute>(); if (bsonElementAttribute != null) { return bsonElementAttribute.ElementName == bsonPropertyName; } return false; }).FirstOrDefault()); }
public List <Book> ListByFeatures(Book b, string orderBy) { //örnek orderBy değeri => title:1 ise title'a göre asc uygular title:-1 ise title'a göre desc uygular. Dictionary <string, object> features = new Dictionary <string, object>(); foreach (PropertyInfo propertyInfo in b.GetType().GetProperties()) { if (propertyInfo.GetValue(b) != null && propertyInfo.GetValue(b).ToString() != "-1" && propertyInfo.GetValue(b).ToString() != "1.01.0001 00:00:00") { object[] attrs = propertyInfo.GetCustomAttributes(true); BsonElementAttribute be = (BsonElementAttribute)attrs[0]; features.Add(be.ElementName, propertyInfo.GetValue(b)); } } if (orderBy != null && orderBy.Split(":").Length == 2) { return(BookCollection.Find(new BsonDocument(features)).Sort("{" + orderBy + "}").ToList()); } return(BookCollection.Find(new BsonDocument(features)).ToList()); }