diff --git a/Chrosey.Extensions.csproj b/Chrosey.Extensions.csproj index 36b61ba..d952b49 100644 --- a/Chrosey.Extensions.csproj +++ b/Chrosey.Extensions.csproj @@ -52,8 +52,10 @@ + + diff --git a/CollectionExtensions.cs b/CollectionExtensions.cs index 5eba64b..12a953a 100644 --- a/CollectionExtensions.cs +++ b/CollectionExtensions.cs @@ -9,12 +9,12 @@ namespace Chrosey.Extensions { public static class CollectionExtensions { - public static string GetValue(this Dictionary dict,string key) + public static T Get(this Dictionary dict, string key) where T : class { - string value; + T value; if (!dict.TryGetValue(key,out value)) { - return string.Empty; + return null; } return value; } @@ -29,39 +29,49 @@ namespace Chrosey.Extensions { try { - var value = query[condition].ToString(); - if (value.EndsWith("*") && value.StartsWith("*")) + var values = query[condition].Split('|'); + var predicate = PredicateBuilder.False(); + foreach (var value in values) { - collection = collection - .Where(m => m - .GetPropertyValue(condition) - .ToString() - .IndexOf(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase) > -1); - } - else if (value.EndsWith("*")) - { - collection = collection - .Where(m => m - .GetPropertyValue(condition) - .ToString() - .StartsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase)); - } - else if (value.StartsWith("*")) - { - collection = collection - .Where(m => m - .GetPropertyValue(condition) - .ToString() - .EndsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase)); - } - else - { - collection = collection - .Where(m => m - .GetPropertyValue(condition) - .ToString() - .Equals(value, StringComparison.CurrentCultureIgnoreCase)); + if (value.EndsWith("*") && value.StartsWith("*")) + { + predicate = predicate + .Or(m => m + .GetPropertyValue(condition) + .ToString() + .IndexOf(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase) > -1 + ); + } + else if (value.EndsWith("*")) + { + predicate = predicate + .Or(m => m + .GetPropertyValue(condition) + .ToString() + .StartsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase) + ); + } + else if (value.StartsWith("*")) + { + predicate = predicate + .Or(m => m + .GetPropertyValue(condition) + .ToString() + .EndsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase) + ); + } + else + { + predicate = predicate + .Or(m => m + .GetPropertyValue(condition) + .ToString() + .Equals(value, StringComparison.CurrentCultureIgnoreCase) + ); + } } + + collection = collection.AsQueryable().Where(predicate); } catch (NotFoundException) { @@ -71,11 +81,15 @@ namespace Chrosey.Extensions } return collection; } - public static IOrderedEnumerable Sort(this IEnumerable collection, NameValueCollection query) where T : class + public static IOrderedEnumerable Sort(this IEnumerable collection, NameValueCollection query, string defaultSortProperty = "") where T : class { if (string.IsNullOrEmpty(query["sort"])) { + if (!string.IsNullOrEmpty(defaultSortProperty) && collection.First().HasProperty(defaultSortProperty)) + { + return collection.OrderBy(c => c.GetPropertyValue(defaultSortProperty)); + } return collection.OrderBy(i => i); } @@ -113,19 +127,17 @@ namespace Chrosey.Extensions if (string.IsNullOrEmpty(groupBy) || !props.Contains(groupBy)) { - return collection.GroupBy(i => "Alle " + typeof(T).ToString()); + return collection.GroupBy(i => "Alle " + typeof(T).Name); } - var grouped = entferneAb > 0 - ? collection.GroupBy(i => i.GetPropertyValue(groupBy).ToString().Remove(entferneAb)) + return entferneAb > 0 + ? collection.GroupBy(i => i.GetPropertyValue(groupBy).ToString().Remove(Math.Min(entferneAb, i.GetPropertyValue(groupBy).ToString().Length))) : collection.GroupBy(i => i.GetPropertyValue(groupBy)); - - return grouped; } - public static IEnumerable> Organize(this IEnumerable collection, NameValueCollection query) where T : class + public static IEnumerable> Organize(this IEnumerable collection, NameValueCollection query, string defaultSortProperty = "") where T : class { - return collection.Filter(query).Sort(query).GroupBy(query); + return collection.Filter(query).Sort(query, defaultSortProperty).GroupBy(query); } } } diff --git a/PredicateBuilder.cs b/PredicateBuilder.cs new file mode 100644 index 0000000..1aef94c --- /dev/null +++ b/PredicateBuilder.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace Chrosey.Extensions +{ + public static class PredicateBuilder + { + public static Expression> True() { return f => true; } + public static Expression> False() { return f => false; } + + public static Expression> Or(this Expression> expr1, + Expression> expr2) + { + var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast()); + return Expression.Lambda> + (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); + } + + public static Expression> And(this Expression> expr1, + Expression> expr2) + { + var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast()); + return Expression.Lambda> + (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters); + } + } +} diff --git a/Wrapper/ConcurrentQueueWrapper.cs b/Wrapper/ConcurrentQueueWrapper.cs new file mode 100644 index 0000000..3875abe --- /dev/null +++ b/Wrapper/ConcurrentQueueWrapper.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Chrosey.Extensions.Wrapper +{ + public class ConcurrentQueueWrapper + { + private readonly ConcurrentQueue queue = new ConcurrentQueue(); + public event EventHandler Changed; + protected virtual void OnChanged() + { + if (Changed != null) Changed(this, EventArgs.Empty); + } + + public virtual void Enqueue(T item) + { + queue.Enqueue(item); + OnChanged(); + } + public int Count { get { return queue.Count; } } + + public virtual bool TryDequeue(out T item) + { + if (queue.TryDequeue(out item)) + { + OnChanged(); + return true; + } + return false; + } + } +}