/// <summary> /// Check if value of specific field is already exist /// </summary> /// <typeparam name="E"></typeparam> /// <param name="fieldName">name of the Field</param> /// <param name="fieldValue">Field value</param> /// <param name="key">Primary key value</param> /// <returns>True or False</returns> public bool TrySameValueExist(string fieldName, object fieldValue, string key) { // First we define the parameter that we are going to use the clause. var xParam = Expression.Parameter(typeof(E), typeof(E).Name); MemberExpression leftExprFieldCheck = MemberExpression.Property(xParam, fieldName); Expression rightExprFieldCheck = Expression.Constant(fieldValue); BinaryExpression binaryExprFieldCheck = MemberExpression.Equal(leftExprFieldCheck, rightExprFieldCheck); MemberExpression leftExprKeyCheck = MemberExpression.Property(xParam, this._KeyProperty); Expression rightExprKeyCheck = Expression.Constant(key); BinaryExpression binaryExprKeyCheck = MemberExpression.NotEqual(leftExprKeyCheck, rightExprKeyCheck); BinaryExpression finalBinaryExpr = Expression.And(binaryExprFieldCheck, binaryExprKeyCheck); //Create Lambda Expression for the selection Expression <Func <E, bool> > lambdaExpr = Expression.Lambda <Func <E, bool> >(finalBinaryExpr, new ParameterExpression[] { xParam }); //Searching .... return(_ctx.CreateQuery <E>("[" + this.GetEntitySetName(typeof(E).Name) + "]").Any <E> (lambdaExpr)); }
/// <summary> /// Verifica si el valor de un campo especifico ya existe. /// </summary> /// <param name="fieldName">Nombre del Field</param> /// <param name="fieldValue">Valor del campo</param> /// <param name="key">Valor de la llave primaria</param> /// <returns>True or False</returns> public bool TrySameValueExist(string fieldName, object fieldValue, string key) { // First we define the parameter that we are going to use the clause. var xParam = Expression.Parameter(typeof(TEntity), typeof(TEntity).Name); //TODO: Maybe it is needed use GetEntitySetName MemberExpression leftExprFieldCheck = MemberExpression.Property(xParam, fieldName); Expression rightExprFieldCheck = Expression.Constant(fieldValue); BinaryExpression binaryExprFieldCheck = MemberExpression.Equal(leftExprFieldCheck, rightExprFieldCheck); MemberExpression leftExprKeyCheck = MemberExpression.Property(xParam, this._keyProperty); Expression rightExprKeyCheck = Expression.Constant(key); BinaryExpression binaryExprKeyCheck = MemberExpression.NotEqual(leftExprKeyCheck, rightExprKeyCheck); BinaryExpression finalBinaryExpr = Expression.And(binaryExprFieldCheck, binaryExprKeyCheck); //Create Lambda Expression for the selection Expression <Func <TEntity, bool> > lambdaExpr = Expression.Lambda <Func <TEntity, bool> >(finalBinaryExpr, new ParameterExpression[] { xParam }); //Searching .... return(((IRepository <TEntity>) this).TryEntity(new Specification <TEntity>(lambdaExpr))); }
/// <summary> /// Get Entity By Primary Key /// </summary> /// <typeparam name="E">Entity Type</typeparam> /// <param name="Key">Primary Key Value</param> /// <returns>return entity</returns> public E SelectByKey(string Key) { // First we define the parameter that we are going to use the clause. var xParam = Expression.Parameter(typeof(E), typeof(E).Name); MemberExpression leftExpr = MemberExpression.Property(xParam, this._KeyProperty); Expression rightExpr; if (leftExpr.Type == typeof(Int32)) { rightExpr = Expression.Constant(Int32.Parse(Key)); } else { rightExpr = Expression.Constant(Key); } BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr); //Create Lambda Expression for the selection Expression <Func <E, bool> > lambdaExpr = Expression.Lambda <Func <E, bool> >(binaryExpr, new ParameterExpression[] { xParam }); //Searching .... var resultCollection = (ObjectQuery <E>)_ctx.CreateQuery <E>("[" + this.GetEntitySetName(typeof(E).Name) + "]") .Where(lambdaExpr); if (null != resultCollection && resultCollection.Count() > 0) { //return valid single result return(resultCollection.First <E>()); }//end if return(null); }
/// <summary> /// Get Entity By Primary key /// </summary> /// <typeparam name="E">Entity Type</typeparam> /// <param name="key">Primary key Value</param> /// <returns>return entity</returns> public TEntity GetByKey(int key) { // First we define the parameter that we are going to use the clause. var xParam = Expression.Parameter(typeof(TEntity), typeof(TEntity).Name); MemberExpression leftExpr = MemberExpression.Property(xParam, this._KeyProperty); Expression rightExpr = Expression.Constant(key); BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr); //Create Lambda Expression for the selection Expression <Func <TEntity, bool> > lambdaExpr = Expression.Lambda <Func <TEntity, bool> >(binaryExpr, new ParameterExpression[] { xParam }); //Searching .... IList <TEntity> resultCollection = ((IRepository <TEntity>) this).SelectAll(lambdaExpr); if (null != resultCollection && resultCollection.Count() > 0) { //return valid single result return(resultCollection.First <TEntity>()); }//end if return(null); }