///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsExpressionTrees.ExpressionListRemoveElementWithGivenKeys``1(System.Object[])"]/*'/> public static Expression <Func <T, bool> > ExpressionListRemoveElementWithGivenKeys <T>(params object[] objs) { var param = Expression.Parameter(typeof(T)); Expression body = Expression.IsFalse(Expression.Equal( Expression.Property( param, typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0]) ), Expression.Constant( objs[0], typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0]).PropertyType ) )); for (int i = 1; i < objs.Length; i++) { body = Expression.Or( body, Expression.IsFalse(Expression.Equal( Expression.Property(param, typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i])), Expression.Constant( objs[i], typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]).PropertyType ) )) ); } // t => t.key1 != value1 || t.key2 != value2 ... return(Expression.Lambda <Func <T, bool> >(body, param)); }
///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.QueryWhereKeysAre``1(System.Linq.IQueryable{``0},System.Object[])"]/*'/> public static IQueryable <T> QueryWhereKeysAre <T>(IQueryable <T> req, params object[] objs) { GenericToolsTypeAnalysis.CheckIfObjectIsKey <T>(objs); if (typeof(T).IsSubclassOf(typeof(BaseEntity))) { int?id = GenericToolsTypeAnalysis.ObjectsToId <T>(objs); req = req.Where( GenericToolsExpressionTrees.PropertyEquals <T>( typeof(T).GetProperty("Id"), id ) ); } else { int i = 0; foreach (object obj in objs) { req = req.Where( GenericToolsExpressionTrees.PropertyEquals <T>( typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]), obj ) ); i++; } } return(req); }
///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.ListWherePropKeysAre``2(System.Collections.Generic.List{``1},System.String,System.Object[])"]/*'/> private static List <Q> ListWherePropKeysAre <T, Q>(List <Q> req, string propname, object[] objs) { GenericToolsTypeAnalysis.CheckIfObjectIsKey <T>(objs); if (typeof(T).IsSubclassOf(typeof(BaseEntity))) { int?id = GenericToolsTypeAnalysis.ObjectsToId <T>(objs); req = req.Where( GenericToolsExpressionTrees.ExpressionPropPropEquals <Q>( typeof(Q).GetProperty(propname), typeof(T).GetProperty("Id"), id ).Compile() ).ToList(); } else { int i = 0; foreach (object obj in objs) { req = req.Where( GenericToolsExpressionTrees.ExpressionPropPropEquals <Q>( typeof(Q).GetProperty(propname), typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]), obj ).Compile() ).ToList(); i++; } } return(req); }
///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsQueriesAndLists.QueryDefaultOrderBy``1(System.Linq.IQueryable{``0})"]/*'/> public static IQueryable <T> QueryDefaultOrderBy <T>(IQueryable <T> req) { string defaultPropertyName; if (typeof(BaseEntity).IsAssignableFrom(typeof(T))) { defaultPropertyName = "Id"; } else { defaultPropertyName = GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0]; } return(QueryOrderByKey(req, defaultPropertyName)); }
///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsExpressionTrees.ExpressionWhereKeysAre``1(System.Object[])"]/*'/> public static Expression <Func <T, bool> > ExpressionWhereKeysAre <T>(params object[] objs) { var param = Expression.Parameter(typeof(T)); Expression body; if (typeof(BaseEntity).IsAssignableFrom(typeof(T))) { int id = (int)objs[0]; body = Expression.Equal(Expression.Property( param, typeof(T).GetProperty("Id") ), Expression.Constant( id, typeof(T).GetProperty("Id").PropertyType )); } else { body = Expression.Equal(Expression.Property( param, typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0]) ), Expression.Constant( objs[0], typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[0]).PropertyType )); for (int i = 1; i < objs.Length; i++) { body = Expression.And(body, Expression.Equal(Expression.Property( param, typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]) ), Expression.Constant( objs[i], typeof(T).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <T>()[i]).PropertyType) )); } } return(Expression.Lambda <Func <T, bool> >(body, param)); }
///<include file='docs.xml' path='doc/members/member[@name="M:GenericRepositoryAndService.Tools.Generic.GenericToolsExpressionTrees.ExpressionListWhereOtherTypePropListNotContains``2(``0,System.String,System.Int32)"]/*'/> public static Expression <Func <Q, bool> > ExpressionListWhereOtherTypePropListNotContains <T, Q>(T newItem, string tPropName, int nbr = 1) { var param = Expression.Parameter(typeof(Q)); var newItemcst = Expression.Constant(newItem, typeof(T)); Expression body = Expression.Property( newItemcst, typeof(T).GetProperty(tPropName) ); var param2 = Expression.Parameter(typeof(Q)); Expression exp2; if (typeof(BaseEntity).IsAssignableFrom(typeof(Q))) { exp2 = Expression.Equal( Expression.Property( param2, typeof(Q).GetProperty("Id") ), Expression.Property( param, typeof(Q).GetProperty("Id") ) ); } else { exp2 = Expression.Equal( Expression.Property( param2, typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[0]) ), Expression.Property( param, typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[0]) ) ); for (int i = 1; i < nbr; i++) { exp2 = Expression.And( exp2, Expression.Equal( Expression.Property( param2, typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[i]) ), Expression.Property( param, typeof(Q).GetProperty(GenericToolsTypeAnalysis.KeyPropertiesNames <Q>()[i]) ) ) ); } } MethodInfo methodWhere = typeof(Enumerable).GetMethods() .Where(m => m.Name == "Where" ) .ToList()[0] .MakeGenericMethod(typeof(Q)); body = Expression.Call( methodWhere, body, Expression.Lambda <Func <Q, bool> >(exp2, param2) ); MethodInfo methodCount = typeof(Enumerable).GetMethods() .Where(m => m.Name == "Count" && m.GetParameters().Length == 1 ) .Single() .MakeGenericMethod(typeof(Q)); body = Expression.Call(methodCount, body); body = Expression.Equal(body, Expression.Constant(1)); body = Expression.Not(body); return(Expression.Lambda <Func <Q, bool> >(body, param)); }