Entity Framework Extensions
.NET library with utility extensions for between operations.
Install the package from nuget
https://nuget.org/packages/efext/
and add the using directive
using EfExt;public static IQueryable<TSource> GreaterThan<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, string>> keySelector,
string value)Returns a subset where a given string column is greater than the given argument
Same as GreaterThan but inclusive
Opposite of GreaterThan
Opposite of GreaterThanOrEqual
Between is the composite of GreaterThanOrEqual and LessThanOrEqual
There are two overloads for the Between method
- Find rows where a single column is between two strings
var subset = ctx.Numbers.Between(i => i.Number, "40401002", "40401004");alternative
var subset = ctx.Numbers.Between("40401002", i => i.Number, "40401004");- Find rows where a string is between two columns
var plan = ctx.NumberPlans.Between(
r => r.LowerNumber,
r => r.UpperNumber,
"40410003");alternative
var plan = ctx.NumberPlans.Between(
r => r.LowerNumber,
"40410003",
r => r.UpperNumber); public static IEnumerable<T> Recursive<T>(
this T node, Func<T, IEnumerable<T>> selector)This method can be used for traversing a tree structure. It will extend the node type, and the provided selector is used for finding the children, that must be of the same type as node (T).
[Test]
public void NodeWithoutChildren()
{
var noChildren = _tree.Recursive(n => n.Children)
.Where(n => n.Children.Empty());
Assert.AreEqual(4, noChildren.Count());
}How to sort a id/parent object by it's hierarchy
[Test]
public void WithLookupFunction()
{
var items = new[]
{
new Item(2, 1),
new Item(3, 1),
new Item(4, 2),
new Item(1, 1),
new Item(5, 1),
new Item(6, 4)
};
var root = items.Single(x => x.Id == 1);
var sortedByTree = root.Recursive(items.ChildSelector).ToList();
var expected = new[] { 1, 2, 4, 6, 3, 5 };
Assert.IsTrue(expected.SequenceEqual(sortedByTree.Select(i => i.Id)));
}
public class Item
{
public int Id;
public int Parent;
public Item() { }
public Item(int id, int parent)
{
Id = id;
Parent = parent;
}
}
public static class ItemExt
{
public static IEnumerable<Item> ChildSelector(
this IEnumerable<Item> source,
Item item)
{
return source.Where(i => i.Parent == item.Id && i.Parent != i.Id);
}
}See the testproject for more examples.