added foreach with index linq.

This commit is contained in:
David Lebee
2018-04-02 11:47:30 -05:00
parent 48db853236
commit f0ed7d9bd5
6 changed files with 354 additions and 223 deletions
@@ -5,130 +5,6 @@ using System.Linq.Expressions;
namespace PoweredSoft.DynamicLinq.Helpers
{
public class ExpressionParameterGroup
{
public ExpressionParameterGroup Parent { get; set; }
public ParameterExpression Parameter { get; set; }
public List<ExpressionPiece> Pieces { get; set; } = new List<ExpressionPiece>();
public ExpressionParameterGroup(ParameterExpression parameter)
{
Parameter = parameter;
}
public void AddSubPart(ExpressionPiece expressionPart)
{
Pieces.Add(expressionPart);
}
public Expression LastExpression() => LastPiece().Expression;
public ExpressionPiece LastPiece() => Pieces.Last();
public LambdaExpression CreateLambda()
{
var lastExpression = LastPiece().Expression;
var lambda = Expression.Lambda(lastExpression, Parameter);
return lambda;
}
}
public class ExpressionPiece
{
public ExpressionParameterGroup Parameter { get; set; }
public ExpressionPiece Parent { get; set; }
public Expression Expression { get; set; }
public ExpressionPiece(ExpressionParameterGroup parameter, ExpressionPiece parent = null)
{
Parameter = parameter;
Parent = parent;
}
public void Resolve(string piece)
{
Expression = Expression.PropertyOrField(Parent?.Expression ?? Parameter.Parameter, piece);
}
public bool IsGenericEnumerable() => QueryableHelpers.IsGenericEnumerable(Expression);
public Type GetGenericEnumerableType() => Expression.Type.GenericTypeArguments.First();
}
public class ExpressionParser
{
public ParameterExpression Parameter { get; protected set; }
public string Path { get; protected set; }
public List<ExpressionParameterGroup> Groups { get; set; } = new List<ExpressionParameterGroup>();
public ExpressionParser(Type type, string path) : this(Expression.Parameter(type), path)
{
}
public ExpressionParser(ParameterExpression parameter, string path)
{
if (parameter == null)
throw new ArgumentNullException("parameter");
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
Parameter = parameter;
Path = path;
}
public void Parse()
{
Groups = new List<ExpressionParameterGroup>();
var pieces = Path.Split('.').ToList();
// create the current parameter.
var currentGroup = CreateAndAddParameterGroup(Parameter);
ExpressionPiece parentPiece = null;
int indexOfPiece = -1;
pieces.ForEach(piece =>
{
++indexOfPiece;
bool isLast = indexOfPiece == pieces.Count - 1;
var expressionPiece = new ExpressionPiece(currentGroup, parentPiece);
expressionPiece.Resolve(piece);
currentGroup.AddSubPart(expressionPiece);
// rest is only if its not the last piece.
if (isLast) return;
if (expressionPiece.IsGenericEnumerable())
{
var param = Expression.Parameter(expressionPiece.GetGenericEnumerableType());
currentGroup = CreateAndAddParameterGroup(param, currentGroup);
parentPiece = null;
}
else
{
parentPiece = expressionPiece;
}
});
}
public ExpressionParameterGroup CreateAndAddParameterGroup(ParameterExpression parameter, ExpressionParameterGroup parent = null)
{
var group = new ExpressionParameterGroup(parameter);
if (parent != null)
group.Parent = parent;
Groups.Add(group);
return group;
}
public ExpressionParameterGroup LastGroup()
{
return this.Groups.Last();
}
}
public class ExpressionPathPart
{
public Expression ParentExpression { get; set; }
@@ -46,5 +46,33 @@ namespace PoweredSoft.DynamicLinq
public static IQueryable GroupBy(this IEnumerable list, Type type, Action<GroupBuilder> callback)
=> list.AsQueryable().GroupBy(type, callback);
public static List<T> Reversed<T>(this List<T> list)
{
var copy = list.ToList();
copy.Reverse();
return copy;
}
public delegate void ForEachDelegate<T>(T element, int index);
public static void ForEach<T>(this List<T> list, ForEachDelegate<T> callback)
{
for (var i = 0; i < list.Count; i++)
callback(list[i], i);
}
public static void ReversedForEach<T>(this List<T> list, Action<T> action)
{
list.Reversed().ForEach(action);
}
public static void ReversedForEach<T>(this List<T> list, ForEachDelegate<T> callback)
{
var reversed = list.Reversed();
reversed.ForEach(callback);
}
}
}