Implemented DynamicNodeList.InGroupsOf, DynamicNodeList.GroupedInto and DynamicNodeList.GroupBy
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
|
||||
namespace umbraco.MacroEngines
|
||||
{
|
||||
public class DynamicGrouping : IEnumerable
|
||||
{
|
||||
public IEnumerable<Grouping<object, DynamicNode>> Inner;
|
||||
|
||||
public DynamicGrouping OrderBy(string expression)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public DynamicGrouping(DynamicNodeList list, string groupBy)
|
||||
{
|
||||
Inner =
|
||||
list
|
||||
.Items
|
||||
.GroupBy(item =>
|
||||
{
|
||||
object result = null;
|
||||
item.TryGetMember(new DynamicQueryableGetMemberBinder(groupBy, false), out result);
|
||||
return result;
|
||||
})
|
||||
.Select(item => new Grouping<object, DynamicNode>()
|
||||
{
|
||||
Key = item.Key,
|
||||
Elements = item.Select(inner => inner)
|
||||
});
|
||||
}
|
||||
public DynamicGrouping(IEnumerable<Grouping<object, DynamicNode>> source)
|
||||
{
|
||||
this.Inner = source;
|
||||
}
|
||||
|
||||
public IEnumerator GetEnumerator()
|
||||
{
|
||||
return Inner.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,7 @@ namespace umbraco.MacroEngines
|
||||
int groupSize = 0;
|
||||
if (int.TryParse(args.First().ToString(), out groupSize))
|
||||
{
|
||||
result = new DynamicNodeList(this.InGroupsOf<DynamicNode>(groupSize).ToList());
|
||||
result = this.InGroupsOf<DynamicNode>(groupSize);
|
||||
return true;
|
||||
}
|
||||
result = new DynamicNull();
|
||||
@@ -71,7 +71,7 @@ namespace umbraco.MacroEngines
|
||||
int groupCount = 0;
|
||||
if (int.TryParse(args.First().ToString(), out groupCount))
|
||||
{
|
||||
result = new DynamicNodeList(this.GroupedInto<DynamicNode>(groupCount).ToList());
|
||||
result = this.GroupedInto<DynamicNode>(groupCount);
|
||||
return true;
|
||||
}
|
||||
result = new DynamicNull();
|
||||
@@ -79,7 +79,7 @@ namespace umbraco.MacroEngines
|
||||
}
|
||||
if (name == "GroupBy")
|
||||
{
|
||||
result = new DynamicNodeList(this.GroupBy<DynamicNode>(args.First().ToString()).ToList());
|
||||
result = this.GroupBy<DynamicNode>(args.First().ToString());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -252,20 +252,38 @@ namespace umbraco.MacroEngines
|
||||
{
|
||||
return ((IQueryable<T>)Items.AsQueryable()).OrderBy(key);
|
||||
}
|
||||
public IQueryable<T> GroupBy<T>(string key)
|
||||
public DynamicGrouping GroupBy<T>(string key)
|
||||
{
|
||||
//return ((IQueryable<T>)Items.AsQueryable()).OrderBy(key);
|
||||
return null;
|
||||
DynamicGrouping group = new DynamicGrouping(this, key);
|
||||
return group;
|
||||
}
|
||||
public IQueryable<T> GroupedInto<T>(int groupCount)
|
||||
public DynamicGrouping GroupedInto<T>(int groupCount)
|
||||
{
|
||||
//return ((IQueryable<T>)Items.AsQueryable()).OrderBy(key);
|
||||
return null;
|
||||
int groupSize = (int)Math.Ceiling(((decimal)Items.Count / groupCount));
|
||||
return new DynamicGrouping(
|
||||
this
|
||||
.Items
|
||||
.Select((node, index) => new KeyValuePair<int, DynamicNode>(index, node))
|
||||
.GroupBy(kv => (object)(kv.Key / groupSize))
|
||||
.Select(item => new Grouping<object, DynamicNode>()
|
||||
{
|
||||
Key = item.Key,
|
||||
Elements = item.Select(inner => inner.Value)
|
||||
}));
|
||||
}
|
||||
public IQueryable<T> InGroupsOf<T>(int groupSize)
|
||||
public DynamicGrouping InGroupsOf<T>(int groupSize)
|
||||
{
|
||||
//return ((IQueryable<T>)Items.AsQueryable()).OrderBy(key);
|
||||
return null;
|
||||
return new DynamicGrouping(
|
||||
this
|
||||
.Items
|
||||
.Select((node, index) => new KeyValuePair<int, DynamicNode>(index, node))
|
||||
.GroupBy(kv => (object)(kv.Key / groupSize))
|
||||
.Select(item => new Grouping<object, DynamicNode>()
|
||||
{
|
||||
Key = item.Key,
|
||||
Elements = item.Select(inner => inner.Value)
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
public IQueryable Select(string predicate, params object[] values)
|
||||
|
||||
24
umbraco.MacroEngines.Juno/RazorDynamicNode/Grouping.cs
Normal file
24
umbraco.MacroEngines.Juno/RazorDynamicNode/Grouping.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
|
||||
namespace umbraco.MacroEngines
|
||||
{
|
||||
public class Grouping<K, T> : IGrouping<K, T>
|
||||
{
|
||||
public K Key { get; set; }
|
||||
public IEnumerable<T> Elements;
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return Elements.GetEnumerator();
|
||||
}
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return (IEnumerator)GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -73,6 +73,7 @@
|
||||
<Compile Include="RazorCore\BaseContext.cs" />
|
||||
<Compile Include="RazorDynamicNode\DynamicBackingItem.cs" />
|
||||
<Compile Include="RazorDynamicNode\DynamicBackingItemType.cs" />
|
||||
<Compile Include="RazorDynamicNode\DynamicGrouping.cs" />
|
||||
<Compile Include="RazorDynamicNode\DynamicMedia.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
@@ -81,6 +82,7 @@
|
||||
</Compile>
|
||||
<Compile Include="RazorDynamicNode\DynamicNodeList.cs" />
|
||||
<Compile Include="RazorDynamicNode\ExamineBackedMedia.cs" />
|
||||
<Compile Include="RazorDynamicNode\Grouping.cs" />
|
||||
<Compile Include="RazorDynamicNode\HtmlTagWrapper.cs" />
|
||||
<Compile Include="RazorDynamicNode\HtmlTagWrapperBase.cs" />
|
||||
<Compile Include="RazorDynamicNode\HtmlTagWrapperTextNode.cs" />
|
||||
|
||||
Reference in New Issue
Block a user