private static object Get <TPrimaryKey>(this IQueryLite query, TPrimaryKey id) { Type entityType = query.EntityType; EntityMetadata entityMetadata = entityType.GetEntityMetadata(); ICollection <string> primaryKeyFieldNames = entityMetadata.PrimaryKeyPropertyNames; if (primaryKeyFieldNames.Count == 0) { throw new InvalidOperationException(string.Format("cannot get {0} by {1} ID, because it has no primary key", entityType.Name, typeof(TPrimaryKey).Name)); } if (primaryKeyFieldNames.Count > 1) { throw new InvalidOperationException(string.Format("cannot get {0} by {1} ID, because its primary key is multiple", entityType.Name, typeof(TPrimaryKey).Name)); } string primaryKeyFieldName = primaryKeyFieldNames.First(); PropertyInfo pi = entityMetadata.Properties[primaryKeyFieldName].PropertyInfo; if (pi == null || pi.PropertyType != typeof(TPrimaryKey)) { throw new InvalidOperationException(string.Format("cannot get {0} by {1} ID, because its primary key is not an {1}", entityType.Name, typeof(TPrimaryKey).Name)); } query.Filter.Add(new ConditionLite { FieldName = primaryKeyFieldName, LogicalOperator = LogicalOperatorLite.And, Operator = OperatorLite.Equals, FieldValue = id }); return(query.FirstOrDefault()); }
//public static IQueryLite<TEntity> OrderByDesc<TEntity, TProperty>(this IQueryLite<TEntity> query, Expression<Func<TEntity, TProperty>> selector) //{ // return query.OrderByDesc(selector.GetMemberName()); //} public static object Get(this IQueryLite query, object ID) { string primaryKeyFieldName = query.EntityType.GetPrimaryKeyFieldName(); var condition = new ConditionLite { FieldName = primaryKeyFieldName, Operator = OperatorLite.Equals, FieldValue = ID }; query.Filter.Add(condition); return(query.FirstOrDefault()); }
public static object Get(this IQueryLite query, object ID) { if (query == null) throw new ArgumentNullException(nameof(query)); string primaryKeyFieldName = query.EntityType.GetPrimaryKeyFieldName(); var condition = new ConditionLite { FieldName = primaryKeyFieldName, Operator = OperatorLite.Equals, FieldValue = ID }; query.Filter.Add(condition); return query.FirstOrDefault(); }