You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I implemented this function in my base repository and this helps me a lot by making searches easier.
I believe that From-queries helps too, it's even better i believe, but I'm just leaving it here because maybe it can help someone.
Function that returns entity few properties base on a predicate.
Returns a list
publicvirtualasyncTask<List<TEntity>>SelectAsync(Func<TEntity,object>properties,Expression<Func<TEntity,bool>>predicate){if(properties==null){thrownew ArgumentNullException(nameof(properties));}// Invoke the selector expression to obtain an object instance of anonymous typeobjectobj= properties.Invoke(NewEntityFunc());
PropertyInfo[]paramProps= obj.GetType().GetProperties();List<PropertyInfo>selectedProps=new();
PropertyInfo[]entityProps=typeof(TEntity).GetProperties();
selectedProps.AddRange(fromparamPropinparamPropsfromentityPropinentityPropswhere entityProp.Name == paramProp.Name
selectentityProp);IEnumerable<string>columns= selectedProps.Select(p => Resolvers.Column(p, _conn));stringtableName= Resolvers.Table(typeof(TEntity), _conn);stringquerySql=$"select {string.Join(", ", columns)} from {tableName}";DynamicParametersqueryParameters=new();if(predicate!=null){SqlExpression<TEntity>sqlexp=new(DommelMapper.GetSqlBuilder(_conn));querySql+=""+ sqlexp.Where(predicate).ToSql(out queryParameters);}IEnumerable<TEntity>data=await _conn.QueryAsync<TEntity>(querySql, queryParameters, _transaction);return data.ToList();}
I implemented this function in my base repository and this helps me a lot by making searches easier.
I believe that From-queries helps too, it's even better i believe, but I'm just leaving it here because maybe it can help someone.
Function that returns entity few properties base on a predicate.
Returns a list
Returns single object
Usage
List
Single object
The text was updated successfully, but these errors were encountered: