예제 #1
0
 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,
     });
 }
예제 #2
0
 public static string TryReadString(this BsonDocument doc, string name)
 {
     if (!doc.ExistFiled(name) || doc[name].IsBsonNull)
     {
         return(string.Empty);
     }
     return(doc[name].ToString());
 }
예제 #3
0
 public static long TryReadLong(this BsonDocument doc, string name)
 {
     if (!doc.ExistFiled(name) || doc[name].IsBsonNull)
     {
         return(0);
     }
     return((long)doc[name]);
 }
예제 #4
0
 public static bool TryReadBool(this BsonDocument doc, string name)
 {
     if (!doc.ExistFiled(name) || doc[name].IsBsonNull)
     {
         return(false);
     }
     return((bool)doc[name]);
 }
예제 #5
0
 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));
     }
 }
예제 #6
0
 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());
     }
 }
예제 #7
0
 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); }
 }
예제 #8
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); }
 }
예제 #9
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));
        }
예제 #10
0
        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);
        }
예제 #11
0
 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,
     });
 }