public void TestEncodingWithPrefixAndTranslationDictionaryAfterDeserialization() { IDictionary <Expression <Func <SortClass, object> >, string> translationDictionary = new Dictionary <Expression <Func <SortClass, object> >, string> { { s => s.A, "0" }, { s => s.B, "1" }, }; Sorter <SortClass> sorter = new Sorter <SortClass>("e", translationDictionary); sorter = SerializeAndDeserializeSorter(sorter); sorter.AddProperty(x => x.A, true); sorter.AddProperty(x => x.B, true); Assert.AreEqual("e.0'e.1", sorter.Encode()); sorter.SortBy(x => x.A, false).SortBy(x => x.B, false); Assert.AreEqual("e.1!'e.0!", sorter.Encode()); sorter.SortBy(x => x.A, true); Assert.AreEqual("e.0'e.1!", sorter.Encode()); }
public void TestPrefixedDecodingAndSorting() { IList <SortClass> data = CreateSimpleSortData(); Sorter <SortClass> sorter = new Sorter <SortClass>("p", "p.A", "p.B!"); List <SortClass> sortedData = data.AsQueryable().OrderBy(sorter).ToList(); Assert.AreSame(data[3], sortedData[0]); Assert.AreSame(data[1], sortedData[1]); Assert.AreSame(data[2], sortedData[2]); Assert.AreSame(data[0], sortedData[3]); Assert.AreSame(data[4], sortedData[4]); sorter = Sorter <SortClass> .Decode("p", "p.A'p.B!"); sortedData = data.AsQueryable().OrderBy(sorter).ToList(); Assert.AreSame(data[3], sortedData[0]); Assert.AreSame(data[1], sortedData[1]); Assert.AreSame(data[2], sortedData[2]); Assert.AreSame(data[0], sortedData[3]); Assert.AreSame(data[4], sortedData[4]); }
public void TestDeepSort() { List <SortClass> data = new List <SortClass> { new SortClass("Daniel", "Apple", new SortClass("Microsoft", "Socks")), //0 new SortClass("Daniel", "Peach", new SortClass("Microsoft", "Socks")), //1 new SortClass("Daniel", "Orange", new SortClass("Atlassian", "Hat")), //2 new SortClass("Alex", "Apple", new SortClass("Atlassian", "Pants")), //3 new SortClass("Dwain", "Strawberry", new SortClass("JetBrains", "Jumper")) //4 }; Sorter <SortClass> sorter = new Sorter <SortClass>() .AddProperty(s => s.C.A, true) .AddProperty(s => s.C.B, true) .AddProperty(s => s.B, false); List <SortClass> sortedData = data.AsQueryable().OrderBy(sorter).ToList(); Assert.AreSame(data[2], sortedData[0]); Assert.AreSame(data[3], sortedData[1]); Assert.AreSame(data[4], sortedData[2]); Assert.AreSame(data[1], sortedData[3]); Assert.AreSame(data[0], sortedData[4]); }
public void TestTranslationDictionaryDecodingAndSortingAfterDeserialization() { IList <SortClass> data = CreateSimpleSortData(); IDictionary <Expression <Func <SortClass, object> >, string> translationDictionary = new Dictionary <Expression <Func <SortClass, object> >, string> { { s => s.A, "0" }, { s => s.B, "1" }, }; Sorter <SortClass> sorter = new Sorter <SortClass>("n", translationDictionary, "n.0", "n.1!"); sorter = SerializeAndDeserializeSorter(sorter); List <SortClass> sortedData = data.AsQueryable().OrderBy(sorter).ToList(); Assert.AreSame(data[3], sortedData[0]); Assert.AreSame(data[1], sortedData[1]); Assert.AreSame(data[2], sortedData[2]); Assert.AreSame(data[0], sortedData[3]); Assert.AreSame(data[4], sortedData[4]); translationDictionary = new Dictionary <Expression <Func <SortClass, object> >, string> { { s => s.A, "0" }, }; sorter = Sorter <SortClass> .Decode("n", translationDictionary, "n.0'n.B!"); sortedData = data.AsQueryable().OrderBy(sorter).ToList(); Assert.AreSame(data[3], sortedData[0]); Assert.AreSame(data[1], sortedData[1]); Assert.AreSame(data[2], sortedData[2]); Assert.AreSame(data[0], sortedData[3]); Assert.AreSame(data[4], sortedData[4]); }
/// <summary> /// Orders the query results by the settings set in the specified <see cref="Sorter{T}"/> /// </summary> /// <typeparam name="TSource">The type being queried</typeparam> /// <param name="queryable">The <see cref="IQueryable{T}"/></param> /// <param name="sorter">The <see cref="Sorter{T}"/> to order by with</param> /// <returns>The <see cref="IQueryable{T}"/> with ordering</returns> /// <exception cref="SorterException"> /// If the Sorter passed in has no properties added to it for sorting. /// </exception> public static IOrderedQueryable <TSource> OrderBy <TSource>(this IQueryable <TSource> queryable, Sorter <TSource> sorter) { if (sorter.OrderedPropertySelectors.Count == 0) { throw new SorterException("No properties have been configured for sorting in the specified sorter!"); } KeyValuePair <LambdaExpression, bool> propertySelectorPair = sorter.OrderedPropertySelectors[0]; string methodName = propertySelectorPair.Value ? "OrderBy" : "OrderByDescending"; IOrderedQueryable <TSource> newQueryable = ApplyQuery(propertySelectorPair.Key, methodName, queryable); for (int i = 1; i < sorter.OrderedPropertySelectors.Count; i++) { propertySelectorPair = sorter.OrderedPropertySelectors[i]; methodName = propertySelectorPair.Value ? "ThenBy" : "ThenByDescending"; newQueryable = ApplyQuery(propertySelectorPair.Key, methodName, newQueryable); } return(newQueryable); }