Ejemplo n.º 1
0
    static IOrderedQueryable <T> ApplyOrder <T>(IQueryable <T> source, string property, string methodName)
    {
        property = property.Trim();
        string[]            props = property.Split('.');
        Type                type  = typeof(T);
        ParameterExpression arg   = Expression.Parameter(type, "x");
        Expression          expr  = arg;

        foreach (string prop in props)
        {
            // use reflection (not ComponentModel) to mirror LINQ
            PropertyInfo pi = type.GetProperty(prop);
            expr = Expression.Property(expr, pi);
            type = pi.PropertyType;
        }
        bool IsString = type.FullName == "System.String";

        Type             delegateType = typeof(Func <,>).MakeGenericType(typeof(T), type);
        MyStringComparer comparer     = new MyStringComparer();
        LambdaExpression lambda       = Expression.Lambda(delegateType, expr, arg);
        object           result       = typeof(Queryable).GetMethods().Single(
            method => method.Name == methodName &&
            method.IsGenericMethodDefinition &&
            method.GetGenericArguments().Length == 2 &&
            (IsString ? method.GetParameters().Length == 3 : method.GetParameters().Length == 2))
                                        .MakeGenericMethod(typeof(T), type)
                                        .Invoke(null, IsString ? new object[] { source, lambda, comparer } : new object[] { source, lambda });

        return((IOrderedQueryable <T>)result);
    }
Ejemplo n.º 2
0
    public static void Main()
    {
        // Creates and initializes an array of strings to sort.
        String[] myArr = new String[9] {
            "cant", "bill's", "coop", "cannot", "billet", "can't", "con", "bills", "co-op"
        };
        Console.WriteLine("\nInitially,");
        foreach (String myStr in myArr)
        {
            Console.WriteLine(myStr);
        }

        // Creates and initializes a Comparer to use.
        //CultureInfo myCI = new CultureInfo( "en-US", false );
        MyStringComparer myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.None);

        // Sorts the array without StringSort.
        Array.Sort(myArr, myComp);
        Console.WriteLine("\nAfter sorting without CompareOptions.StringSort:");
        foreach (String myStr in myArr)
        {
            Console.WriteLine(myStr);
        }

        // Sorts the array with StringSort.
        myComp = new MyStringComparer(CompareInfo.GetCompareInfo("en-US"), CompareOptions.StringSort);
        Array.Sort(myArr, myComp);
        Console.WriteLine("\nAfter sorting with CompareOptions.StringSort:");
        foreach (String myStr in myArr)
        {
            Console.WriteLine(myStr);
        }
    }
            public void AEqualB()
            {
                const string A = "a";
                const string B = "b";
                var comparer = new MyStringComparer();

                Assert.Equal(A, B, comparer);
            }
            public void AEqualA()
            {
                const string A = "a";
                const string AlsoA = "a";
                var comparer = new MyStringComparer();

                Assert.Equal(A, AlsoA, comparer);
            }
            public void ExpectedItemUsingComparer()
            {
                const string Expected = "my string";
                IEnumerable<string> myCollection = new List<string> { Expected, "test", "another test" };
                IEqualityComparer<string> comparer = new MyStringComparer();

                Assert.Contains(Expected, myCollection, comparer);
            }
            public void UnexpectedItemUsingComparer()
            {
                const string Unexpected = "test";
                IEnumerable<string> myCollection = new List<string> { "another test" };
                IEqualityComparer<string> comparer = new MyStringComparer();

                Assert.DoesNotContain(Unexpected, myCollection, comparer);
            }
            public void UnexpectedItemUsingComparer()
            {
                IEnumerable<string> myCollection = new List<string> { "test", "another test" };
                const string Expected = "Totally not in the collection!";
                IEqualityComparer<string> comparer = new MyStringComparer();

                Assert.Contains(Expected, myCollection, comparer);
            }