Added PredicateBuilder
This commit is contained in:
parent
6a273efd08
commit
c94e357006
@ -52,8 +52,10 @@
|
|||||||
<Compile Include="Exceptions\NotFoundException.cs" />
|
<Compile Include="Exceptions\NotFoundException.cs" />
|
||||||
<Compile Include="NumberExtensions.cs" />
|
<Compile Include="NumberExtensions.cs" />
|
||||||
<Compile Include="ColorExtensions.cs" />
|
<Compile Include="ColorExtensions.cs" />
|
||||||
|
<Compile Include="PredicateBuilder.cs" />
|
||||||
<Compile Include="StringExtensions.cs" />
|
<Compile Include="StringExtensions.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Wrapper\ConcurrentQueueWrapper.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup />
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
|||||||
@ -9,12 +9,12 @@ namespace Chrosey.Extensions
|
|||||||
{
|
{
|
||||||
public static class CollectionExtensions
|
public static class CollectionExtensions
|
||||||
{
|
{
|
||||||
public static string GetValue(this Dictionary<string,string> dict,string key)
|
public static T Get<T>(this Dictionary<string, T> dict, string key) where T : class
|
||||||
{
|
{
|
||||||
string value;
|
T value;
|
||||||
if (!dict.TryGetValue(key,out value))
|
if (!dict.TryGetValue(key,out value))
|
||||||
{
|
{
|
||||||
return string.Empty;
|
return null;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@ -29,40 +29,50 @@ namespace Chrosey.Extensions
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var value = query[condition].ToString();
|
var values = query[condition].Split('|');
|
||||||
|
var predicate = PredicateBuilder.False<T>();
|
||||||
|
foreach (var value in values)
|
||||||
|
{
|
||||||
if (value.EndsWith("*") && value.StartsWith("*"))
|
if (value.EndsWith("*") && value.StartsWith("*"))
|
||||||
{
|
{
|
||||||
collection = collection
|
predicate = predicate
|
||||||
.Where(m => m
|
.Or(m => m
|
||||||
.GetPropertyValue(condition)
|
.GetPropertyValue(condition)
|
||||||
.ToString()
|
.ToString()
|
||||||
.IndexOf(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase) > -1);
|
.IndexOf(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase) > -1
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else if (value.EndsWith("*"))
|
else if (value.EndsWith("*"))
|
||||||
{
|
{
|
||||||
collection = collection
|
predicate = predicate
|
||||||
.Where(m => m
|
.Or(m => m
|
||||||
.GetPropertyValue(condition)
|
.GetPropertyValue(condition)
|
||||||
.ToString()
|
.ToString()
|
||||||
.StartsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase));
|
.StartsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else if (value.StartsWith("*"))
|
else if (value.StartsWith("*"))
|
||||||
{
|
{
|
||||||
collection = collection
|
predicate = predicate
|
||||||
.Where(m => m
|
.Or(m => m
|
||||||
.GetPropertyValue(condition)
|
.GetPropertyValue(condition)
|
||||||
.ToString()
|
.ToString()
|
||||||
.EndsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase));
|
.EndsWith(value.Trim('*'), StringComparison.CurrentCultureIgnoreCase)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
collection = collection
|
predicate = predicate
|
||||||
.Where(m => m
|
.Or(m => m
|
||||||
.GetPropertyValue(condition)
|
.GetPropertyValue(condition)
|
||||||
.ToString()
|
.ToString()
|
||||||
.Equals(value, StringComparison.CurrentCultureIgnoreCase));
|
.Equals(value, StringComparison.CurrentCultureIgnoreCase)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
collection = collection.AsQueryable().Where(predicate);
|
||||||
|
}
|
||||||
catch (NotFoundException)
|
catch (NotFoundException)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -71,11 +81,15 @@ namespace Chrosey.Extensions
|
|||||||
}
|
}
|
||||||
return collection;
|
return collection;
|
||||||
}
|
}
|
||||||
public static IOrderedEnumerable<T> Sort<T>(this IEnumerable<T> collection, NameValueCollection query) where T : class
|
public static IOrderedEnumerable<T> Sort<T>(this IEnumerable<T> collection, NameValueCollection query, string defaultSortProperty = "") where T : class
|
||||||
{
|
{
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(query["sort"]))
|
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);
|
return collection.OrderBy(i => i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,19 +127,17 @@ namespace Chrosey.Extensions
|
|||||||
|
|
||||||
if (string.IsNullOrEmpty(groupBy) || !props.Contains(groupBy))
|
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
|
return entferneAb > 0
|
||||||
? collection.GroupBy(i => i.GetPropertyValue(groupBy).ToString().Remove(entferneAb))
|
? collection.GroupBy(i => i.GetPropertyValue(groupBy).ToString().Remove(Math.Min(entferneAb, i.GetPropertyValue(groupBy).ToString().Length)))
|
||||||
: collection.GroupBy(i => i.GetPropertyValue(groupBy));
|
: 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
|
public static IEnumerable<IGrouping<object, T>> Organize<T>(this IEnumerable<T> 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
PredicateBuilder.cs
Normal file
31
PredicateBuilder.cs
Normal file
@ -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<Func<T, bool>> True<T>() { return f => true; }
|
||||||
|
public static Expression<Func<T, bool>> False<T>() { return f => false; }
|
||||||
|
|
||||||
|
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
|
||||||
|
Expression<Func<T, bool>> expr2)
|
||||||
|
{
|
||||||
|
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
|
||||||
|
return Expression.Lambda<Func<T, bool>>
|
||||||
|
(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
|
||||||
|
Expression<Func<T, bool>> expr2)
|
||||||
|
{
|
||||||
|
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
|
||||||
|
return Expression.Lambda<Func<T, bool>>
|
||||||
|
(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
Wrapper/ConcurrentQueueWrapper.cs
Normal file
36
Wrapper/ConcurrentQueueWrapper.cs
Normal file
@ -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<T>
|
||||||
|
{
|
||||||
|
private readonly ConcurrentQueue<T> queue = new ConcurrentQueue<T>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user