extensions/CollectionExtensions.cs
2017-04-07 12:36:42 +02:00

132 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Chrosey.Extensions
{
public static class CollectionExtensions
{
public static string GetValue(this Dictionary<string,string> dict,string key)
{
string value;
if (!dict.TryGetValue(key,out value))
{
return string.Empty;
}
return value;
}
public static IEnumerable<T> Filter<T>(this IEnumerable<T> collection, NameValueCollection query) where T : class
{
var props = typeof(T).GetProperties().Select(p => p.Name.ToLower());
var commonProps = props.Intersect(query.AllKeys.Select(k => k.ToLower()));
foreach (var condition in commonProps)
{
try
{
var value = query[condition].ToString();
if (value.EndsWith("*") && value.StartsWith("*"))
{
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));
}
}
catch (NotFoundException)
{
}
}
return collection;
}
public static IOrderedEnumerable<T> Sort<T>(this IEnumerable<T> collection, NameValueCollection query) where T : class
{
if (string.IsNullOrEmpty(query["sort"]))
{
return collection.OrderBy(i => i);
}
var order = query["sort"].Split(',');
var sortProps = order.Select(o => o.Split(' ')[0].ToLower());
var props = typeof(T).GetProperties().Select(p => p.Name.ToLower());
IOrderedEnumerable<T> ordered = null;
foreach (var condition in sortProps.Intersect(props))
{
var isDescending = order
.Single(c => c.StartsWith(condition, StringComparison.CurrentCultureIgnoreCase))
.EndsWith("desc", StringComparison.CurrentCultureIgnoreCase);
if (ordered == null)
{
ordered = isDescending
? collection.OrderByDescending(i => i.GetPropertyValue(condition))
: collection.OrderBy(i => i.GetPropertyValue(condition));
}
else
{
ordered = isDescending
? ordered.ThenByDescending(i => i.GetPropertyValue(condition))
: ordered.ThenBy(i => i.GetPropertyValue(condition));
}
}
return ordered;
}
public static IEnumerable<IGrouping<object, T>> GroupBy<T>(this IEnumerable<T> collection, NameValueCollection query) where T : class
{
var props = typeof(T).GetProperties().Select(p => p.Name.ToLower());
var groupBy = string.IsNullOrEmpty(query["groupby"])? "" : query["groupby"].ToLower();
var entferneAb = string.IsNullOrEmpty(query["groupbynletters"]) ? 0 : int.Parse(query["groupbynletters"]);
if (string.IsNullOrEmpty(groupBy) || !props.Contains(groupBy))
{
return collection.GroupBy(i => "Alle " + typeof(T).ToString());
}
var grouped = entferneAb > 0
? collection.GroupBy(i => i.GetPropertyValue(groupBy).ToString().Remove(entferneAb))
: collection.GroupBy(i => i.GetPropertyValue(groupBy));
return grouped;
}
public static IEnumerable<IGrouping<object, T>> Organize<T>(this IEnumerable<T> collection, NameValueCollection query) where T : class
{
return collection.Filter(query).Sort(query).GroupBy(query);
}
}
}