public KeyInfoBase( MemberInfo[] entityKeyMembers, SortOrder[] sortOrders, IComparer <TKey> keyComparer, IKeyInfoHelper helper) { this.entityKeyMembers = entityKeyMembers; if (sortOrders == null) { sortOrders = Enumerable .Repeat(SortOrder.Ascending, helper.GetMemberCount()) .ToArray(); } this.sortOrders = sortOrders; this.keyComparer = keyComparer; this.keySelector = KeyExpressionHelper .CreateKeySelector <TEntity, TKey>(entityKeyMembers, helper) .Compile(); this.keyEmptinessDetector = KeyExpressionHelper .CreateKeyEmptinessDetector <TEntity, TKey>(helper) .Compile(); }
public static Expression CreateKeyEmptinessDetector( Expression source, IKeyInfoHelper helper) { Expression body = null; int memberCount = helper.GetMemberCount(); for (int i = 0; i < memberCount; i++) { Expression member = helper.CreateKeyMemberSelectorExpression(source, i); Type memberType = member.Type; if (ReflectionHelper.IsNullable(memberType)) { Expression equalityTest = Expression.Equal( member, Expression.Constant(null, memberType)); if (body == null) { body = equalityTest; } else { body = Expression.Or(body, equalityTest); } } } // If all the members of the anonymous type is not nullable, the body expression // is null at this point if (body == null) { body = Expression.Constant(false); } return(body); }
public GenericKeyComparer(SortOrder[] sortOrders, IKeyInfoHelper helper) { if (helper == null) { throw new ArgumentNullException("helper"); } int memberCount = helper.GetMemberCount(); if (memberCount <= 0) { throw new ArgumentException( ExceptionMessages.InvalidKeyInfoHelper, "helper"); } if (sortOrders == null) { sortOrders = Enumerable.Repeat(SortOrder.Ascending, memberCount).ToArray(); } if (sortOrders.Length != memberCount) { throw new ArgumentException( ExceptionMessages.MemberAndSortOrderCountMismatch, "sortOrders"); } ParameterExpression x = Expression.Parameter(typeof(T), "x"); ParameterExpression y = Expression.Parameter(typeof(T), "y"); ParameterExpression variable = Expression.Variable(typeof(int), "var"); List <Expression> blockBody = new List <Expression>(); for (int i = 0; i < memberCount; i++) { Expression xMember = helper.CreateKeyMemberSelectorExpression(x, i); Expression yMember = helper.CreateKeyMemberSelectorExpression(y, i); Expression expr = Expression.Assign( variable, this.CreateComparsion(xMember, yMember, sortOrders[i])); if (0 < i) { expr = Expression.IfThen( Expression.Equal(variable, Expression.Constant(0)), expr); } blockBody.Add(expr); } // Eval the last variable blockBody.Add(variable); var lambda = Expression.Lambda <Func <T, T, int> >( Expression.Block( new ParameterExpression[] { variable }, blockBody), x, y); this.comparer = lambda.Compile(); }