/// <summary> /// Parse traditional SQL order-by statement into a SortKeyList. /// </summary> /// <remarks><para>The order-by input is a comma separated string of fields names with added direction keywords. /// For instance "Title, Age desc". The field names are case-sensitive whereas the direction keywords are case-insensitive. /// Possible values for the direction keywords are "ascending", "descending", "asc", "desc".</para> /// <para>Developers should actually try to avoid using this method. It is primarily here to connect GUI and code where the GUI /// cannot produce the <c>Func<TSource,object></c> objects directly.</para></remarks> /// <param name="orderBy">Order-by string.</param> /// <returns>A SortKeyList representing the order-by string.</returns> public static SortKeyList <TSource> CreateKeyList(string orderBy) { SortKeyList <TSource> list = new SortKeyList <TSource>(); if (orderBy == null) { return(list); } orderBy = orderBy.Trim(); if (orderBy.Length == 0) { return(list); } string[] parts = orderBy.Split(','); Type sourceType = typeof(TSource); foreach (string part in parts) { string[] keydir = part.Trim().Split(' '); string key = keydir[0].Trim(); string dirstr = (keydir.Length > 1 ? keydir[1].Trim() : null); SortKeyDirection dir = ParseDirection(dirstr); list.AddKey(dir, ParseFieldReference(sourceType, key)); } return(list); }
public static SortKeyList <TSource> CreateKeyList(SortKeyDirection direction, Expression <Func <TSource, object> > key) { SortKeyList <TSource> list = new SortKeyList <TSource>(); list.Add(new SortKey <TSource>(direction, key)); return(list); }
protected static SortKeyDirection ParseDirection(string dirstr) { SortKeyDirection dir = SortKeyDirection.Ascending; if (string.IsNullOrEmpty(dirstr)) { return(dir); } if (dirstr.ToLower() == "desc" || dirstr.ToLower() == "descending") { dir = SortKeyDirection.Descending; } else if (dirstr.ToLower() != "asc" && dirstr.ToLower() != "ascending") { throw new ArgumentException(string.Format("Unknown direction keyword '{0}'", dirstr)); } return(dir); }
public SortKey(SortKeyDirection direction, Expression <Func <TSource, object> > key) { Direction = direction; Key = key; }
public SortKeyList <TSource> AddKey(SortKeyDirection direction, Expression <Func <TSource, object> > key) { Add(new SortKey <TSource>(direction, key)); return(this); }