public static Result <ExternalEntity> MostRecent(this ExternalEntity criteria, DateTime?from = null, DateTime?to = null) { var result = new Result <ExternalEntity>(); try { using (var db = new RSMDB.RSMDataModelDataContext()) { var type = Enum.GetName(typeof(EntityType), criteria.EntityType); var rows = criteria.SearchKeys(db, x => x.SystemId == criteria.ExternalSystemId && x.EntityType == type && (from == null || x.Added > from) && (to == null || x.Added < to)).OrderByDescending(o => o.Added); result.Entity = rows.Select(x => x.ToModel <ExternalEntity>()).FirstOrDefault(); if (result.Entity == null) { return(result.Fail("most recent external entity not found.")); } } } catch (Exception e) { return(result.Set(ResultType.TechnicalError, e, "Get AccessLog failed. {0}", e.ToString())); } return(result); }
/// <summary> /// Search by specific criteria and optional date range on when Added. /// </summary> /// <param name="criteria">Fields considered: ExternalSystemId, EntityType</param> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> public static Result <List <ExternalEntity> > SearchKeys(this ExternalEntity criteria, DateTime?from = null, DateTime?to = null) { var result = new Result <List <ExternalEntity> >(); try { using (var db = new RSMDB.RSMDataModelDataContext()) { var type = Enum.GetName(typeof(EntityType), criteria.EntityType); var rows = criteria.SearchKeys(db, x => x.SystemId == criteria.ExternalSystemId && x.EntityType == type && (from == null || x.Added > from) && (to == null || x.Added < to)); result.Entity = rows.Select(x => x.ToModel <ExternalEntity>()).ToList(); } } catch (Exception e) { return(result.Set(ResultType.TechnicalError, e, "Get AccessLog failed. {0}", e.ToString())); } return(result); }