/// <inheritdoc /> /// <summary> /// 批量添加一个实体 /// </summary> /// <param name="entities"></param> /// <param name="primaryKey"></param> /// <param name="commandTimeout"></param> /// <returns></returns> public bool Insert(IEnumerable <T> entities, Expression <Func <T, object> > primaryKey = null, int?commandTimeout = default(int?)) { try { _context.Insert(entities, ExpressionUtils.GetProperty(primaryKey), commandTimeout); return(true); } catch { return(false); } }
/// <summary> /// Initializes a new instance of the <see cref="CopyCrossReferencedCounterPartDeterminationHelper{TCrossReferencedModel,TReferencingModel}"/> class. /// </summary> /// <remarks>It is essential that the "cross-referenced models" are copied before the "referencing models".</remarks> /// <param name="referencingProperty">An expression representing the property on the "referencing model" that refers to the "cross-referenced model".</param> public CopyCrossReferencedCounterPartDeterminationHelper( Expression <Func <TReferencingModel, TCrossReferencedModel> > referencingProperty) { referencingProperty.NotNull(nameof(referencingProperty)); this.referencingProperty = ExpressionUtils.GetProperty(referencingProperty); this.crossReferencedModelPostProcessings = new GenericCopyPostProcessing <TCrossReferencedModel>(this.CrossReferencedModelAction); this.referencingModelPostProcessings = new GenericCopyPostProcessing <TReferencingModel>(this.ReferencingModelAction); this.referencedModelSourceTargetRepository = new Dictionary <TCrossReferencedModel, TCrossReferencedModel>(); }
public static IQueryable <TEntity> OrderBy <TEntity>(this IQueryable <TEntity> source, IEnumerable <ResourceParametersSorting> sorting) { // Get source expression to apply sorting to var expression = source.Expression; // Check if have any sorting properties if (sorting.Count() > 0) { // Initialize succeeded iterations count var iterations = 0; // Iterate sortable properties foreach (var sort in sorting) { try { // Create lambda parameter by type var entity = Expression.Parameter(typeof(TEntity), "m"); // Create property selector expression "m => m.Property.DeepProperty" var property = ExpressionUtils.GetProperty(entity, sort.Property); var propertySelector = Expression.Lambda(property, entity); // Get the method to call var methodName = GetMethodName(sort, iterations == 0); // Create array that represents method return type var returnType = new Type[] { typeof(TEntity), property.Type }; // Call ordering method and store returned expression for next iteration expression = Expression.Call(typeof(Queryable), methodName, returnType, expression, Expression.Quote(propertySelector)); // Increment succeeded iterations count iterations += 1; } catch { // If any exception occurs - just ignore and continue to next property } } // Check if any ordering method has been added to default expression if (iterations > 0) { // Add ordering methods chain to source (source.OrderBy(m => m.Property).ThenBy(m => m.Property.DeepProperty)) return(source.Provider.CreateQuery <TEntity>(expression)); } } // Return unchaged source return(source); }
protected void AddProperty(EntityView entityView, TComponent component, Expression <Func <TComponent, object> > property, bool isReadOnly = true, bool isRequired = false) { var propertyInfo = ExpressionUtils.GetProperty(property); var getter = ExpressionUtils.CreateGetter(property); entityView.Properties.Add(new ViewProperty { Name = propertyInfo.Name, IsRequired = false, RawValue = getter(component), IsReadOnly = false }); }
public static IQueryable <TEntity> Search <TEntity>(this IQueryable <TEntity> source, IEnumerable <KeyValuePair <String, String> > filters) { if (filters == null || filters.Count() == 0) { return(source); } var entity = Expression.Parameter(typeof(TEntity), "m"); var conditions = new List <Expression>(); // Iterate fields foreach (var filter in filters) { try { // Create lambda expression "m.Property.DeepProperty" var property = ExpressionUtils.GetProperty(entity, filter.Key); var propertyValue = ExpressionUtils.GetNormalized(property); // Create lambda expression "value.ToUpper()" var search = Expression.Constant(filter.Value.TrimStart('!')); var searchValue = ExpressionUtils.GetNormalized(search); // Create lambda expression "m.PropertyName.ToUpper().Contains(value.ToUpper())" var contains = Expression.Call(propertyValue, "Contains", null, searchValue); // Create lambda expression "m.PropertyName.ToString().ToUpper().Contains(value.ToUpper()) == (true|false)" var condition = Expression.Equal(contains, filter.Value[0] == '!' ? Expression.Constant(false) : Expression.Constant(true)); // Add resulter expression o expressions list conditions.Add(condition); } catch { } } // Check if have any conditions if (conditions.Count > 0) { // Combine all conditions together var predicateBody = conditions.Skip(1).Aggregate(conditions.FirstOrDefault(), (a, b) => Expression.Or(a, b)); // Create predicate var predicate = Expression.Lambda <Func <TEntity, Boolean> >(predicateBody, entity); // Apply where clause return(source.Where(predicate)); } return(source); }
protected virtual Expression <Func <TEntity, Boolean> > GetKeyComparer(TKey id) { // Get key property name var name = this.context.Model .FindEntityType(typeof(TEntity)) .FindPrimaryKey() .Properties .Select(m => m.Name) .Single(); // Create key property selector expression (m.EntityID) var parameter = Expression.Parameter(typeof(TEntity), "m"); var property = ExpressionUtils.GetProperty(parameter, name); // Create comparer expression (m.EntityID == id) var value = Expression.Constant(id); var equals = Expression.Equal(property, value); // Return key comparer expression (m => m.EntityID == id) return(Expression.Lambda <Func <TEntity, Boolean> >(equals, parameter)); }
private static Expression GetCondition(Expression entity, KeyValuePair <String, String> filter) { // Define list of allowed operators var operators = new List <String> { ">=", "<=", ">", "<", "!?", "!", "?" }; // Initialize value var @operator = String.Empty; var value = filter.Value; // Iterate allowed operators foreach (var entry in operators) { // Check if value contains operator if (value.StartsWith(entry)) { // Store operator @operator = entry; // Remove operator value = value.TrimStart(entry.ToCharArray()); } } // Create lambda expression "m.Property.DeepProperty" var property = ExpressionUtils.GetProperty(entity, filter.Key); // Try convert value type Expression search; // Try substitute some keywords switch (value.Trim().ToLowerInvariant()) { case "null": search = Expression.Constant(null); break; case "now": search = Expression.Constant(DateTime.Now); break; case "empty": search = Expression.Empty(); break; case "default": search = Expression.Default(property.Type); break; default: if (property.Type.IsEnum) { search = Expression.Constant(Enum.Parse(property.Type, value, true)); } else { search = Expression.Constant(Convert.ChangeType(value, property.Type)); } break; } // Return required expression switch (@operator) { case ">=": return(Expression.GreaterThanOrEqual(property, search)); case "<=": return(Expression.LessThanOrEqual(property, search)); case ">": return(Expression.GreaterThan(property, search)); case "<": return(Expression.LessThan(property, search)); case "!?": return(Expression.NotEqual( Expression.Call( ExpressionUtils.GetNormalized(property), "Contains", null, ExpressionUtils.GetNormalized( Expression.Constant(value) ) ), Expression.Constant(true) )); case "!": return(Expression.NotEqual(property, search)); case "?": return(Expression.Call( ExpressionUtils.GetNormalized(property), "Contains", null, ExpressionUtils.GetNormalized(Expression.Constant(value)) )); default: return(Expression.Equal(property, search)); } }
/// <inheritdoc /> /// <summary> /// 添加一个实体 /// </summary> /// <param name="entity"></param> /// <param name="primaryKey"></param> /// <returns></returns> public bool Insert(T entity, Expression <Func <T, object> > primaryKey = null) { return(_context.Insert(entity, ExpressionUtils.GetProperty(primaryKey)) > 0); }
/// <inheritdoc /> /// <summary> /// 根据主键获取一个实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public T Get <TValue>(Expression <Func <T, object> > primaryKey, TValue value) where TValue : struct { return(_context.Get <T, TValue>(ExpressionUtils.GetProperty(primaryKey), value)); }
public static EmitSyntax Stprop <T, R>(this EmitSyntax emit, Expression <Func <T, R> > expr) { var property = ExpressionUtils.GetProperty(expr); return(emit.Call(property.GetSetMethod())); }