public static MealItem FromBson(BsonDocument doc) { if (doc == null) { return(null); } if (!doc.ExistFiled("ID")) { return(null); } return(new MealItem() { ID = doc.TryReadLong("ID"), PlanID = doc.TryReadLong("PlanID"), Name = doc.TryReadString("Name"), Count = doc.TryReadDouble("Count"), NBDID = doc.TryReadString("NBDID"), Unit = doc.TryReadString("Unit"), Tags = doc.TryReadArray("Tags").ToArray(), Type = doc.TryReadString("Type"), Description = doc.TryReadString("Description"), Createtime = doc.TryReadDate("Createtime").Value, UpdateTime = doc.TryReadDate("UpdateTime").Value, }); }
public static string TryReadString(this BsonDocument doc, string name) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull) { return(string.Empty); } return(doc[name].ToString()); }
public static long TryReadLong(this BsonDocument doc, string name) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull) { return(0); } return((long)doc[name]); }
public static bool TryReadBool(this BsonDocument doc, string name) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull) { return(false); } return((bool)doc[name]); }
public static IEnumerable <T> TryReadArray <T>(this BsonDocument doc, string name, Func <BsonValue, T> ctor) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull || !(doc[name].IsBsonArray)) { yield break; } foreach (var p in (BsonArray)doc[name]) { yield return(ctor(p)); } }
public static IEnumerable <string> TryReadArray(this BsonDocument doc, string name) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull || !(doc[name].IsBsonArray)) { yield break; } foreach (var p in (BsonArray)doc[name]) { yield return(p.ToString()); } }
public static double TryReadDouble(this BsonDocument doc, string name) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull) { return(0); } try { return(double.Parse(doc[name].ToString())); } catch { return(0); } }
public static int TryReadInt(this BsonDocument doc, string name) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull) { return(0); } try { return((int)doc[name]); } catch { return(0); } }
public static T TryReadObject <T>(this BsonDocument doc, string name, Func <BsonDocument, T> ctor) { if (!doc.ExistFiled(name) || doc[name].IsBsonNull) { return(default(T)); } var attr = doc[name] as BsonDocument; if (attr == null) { return(default(T)); } return(ctor(attr)); }
public static DateTime?TryReadDate(this BsonDocument doc, string name) { string filedName = name; if (!doc.ExistFiled(name, out filedName) || doc[filedName].IsBsonNull) { return(null); } var value = doc[filedName]; if (value.IsValidDateTime) { return(((BsonDateTime)doc[filedName]).ToUniversalTime()); } return(null); }
public static MealPlan FromBson(BsonDocument doc) { if (doc == null) { return(null); } if (!doc.ExistFiled("ID")) { return(null); } return(new MealPlan() { ID = doc.TryReadLong("ID"), Name = doc.TryReadString("Name"), Tags = doc.TryReadArray("Tags").ToArray(), Description = doc.TryReadString("Description"), Createtime = doc.TryReadDate("Createtime").Value, UpdateTime = doc.TryReadDate("UpdateTime").Value, }); }