Created
March 23, 2021 17:07
-
-
Save nick-hoang/de86d6a720c3eef2fec4d70942605708 to your computer and use it in GitHub Desktop.
Umbraco v8: Helpful methods to query children of an IContent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class IContentExtensions { | |
public static bool HasChildren(this IContent content) | |
{ | |
return Umbraco.Core.Composing.Current.Services.ContentService.HasChildren(content.Id); | |
} | |
public static int CountChildren(this IContent content, string childDocType = null) | |
{ | |
return Umbraco.Core.Composing.Current.Services.ContentService.CountChildren(content.Id, childDocType); | |
} | |
/// <summary> | |
/// Get last children | |
/// </summary> | |
/// <param name="content"></param> | |
/// <param name="order">Default is sortOrder</param> | |
/// <param name="predicate">filter</param> | |
/// <returns></returns> | |
public static IContent LastChildren(this IContent content, | |
string order = "sortOrder", Direction direction = Direction.Ascending, | |
Expression<Func<IContent, bool>> predicate = null) | |
{ | |
long total; | |
IQuery<IContent> filter = null; | |
if (predicate != null) | |
{ | |
var _sqlContext = Umbraco.Core.Composing.Current.Factory.GetInstance<ISqlContext>(); | |
filter = _sqlContext.Query<IContent>().Where(predicate); | |
} | |
Ordering ordering = null; | |
if (!string.IsNullOrEmpty(order)) | |
{ | |
var real_direction = direction == Direction.Ascending ? Direction.Descending : Direction.Ascending; | |
ordering = Ordering.By(order, real_direction, null, false); | |
} | |
return Umbraco.Core.Composing.Current.Services.ContentService.GetPagedChildren(content.Id, 0, 1, out total, filter, ordering).FirstOrDefault(); | |
} | |
/// <summary> | |
/// Get last children | |
/// </summary> | |
/// <param name="content"></param> | |
/// <param name="order">Default is sortOrder</param> | |
/// <param name="predicate">filter</param> | |
/// <returns></returns> | |
public static IContent FirstChildren(this IContent content, | |
string order = "sortOrder", Direction direction = Direction.Ascending, | |
Expression<Func<IContent, bool>> predicate = null) | |
{ | |
long total; | |
IQuery<IContent> filter = null; | |
if (predicate != null) | |
{ | |
var _sqlContext = Umbraco.Core.Composing.Current.Factory.GetInstance<ISqlContext>(); | |
filter = _sqlContext.Query<IContent>().Where(predicate); | |
} | |
Ordering ordering = null; | |
if (!string.IsNullOrEmpty(order)) | |
{ | |
ordering = Ordering.By(order, direction, null, false); | |
} | |
return Umbraco.Core.Composing.Current.Services.ContentService.GetPagedChildren(content.Id, 0, 1, out total, filter, ordering).FirstOrDefault(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="parent"></param> | |
/// <param name="predicate"></param> | |
/// <returns></returns> | |
public static IContent IsExistChildren(this IContent parent, Expression<Func<IContent, bool>> predicate = null) | |
{ | |
long total; | |
IQuery<IContent> filter = null; | |
if (predicate != null) | |
{ | |
var _sqlContext = Umbraco.Core.Composing.Current.Factory.GetInstance<ISqlContext>(); | |
filter = _sqlContext.Query<IContent>().Where(predicate); | |
} | |
var result = Umbraco.Core.Composing.Current.Services.ContentService.GetPagedChildren(parent.Id, 0, 1, out total, filter); | |
return result.FirstOrDefault(); | |
} | |
public static IEnumerable<IContent> Children(this IContent parent, int top, Expression<Func<IContent, bool>> predicate = null, | |
string order = "sortOrder", | |
Direction direction = Direction.Ascending) | |
{ | |
if (top <= 0) return null; | |
long total; | |
IQuery<IContent> filter = null; | |
if (predicate != null) | |
{ | |
var _sqlContext = Umbraco.Core.Composing.Current.Factory.GetInstance<ISqlContext>(); | |
filter = _sqlContext.Query<IContent>().Where(predicate); | |
} | |
Ordering ordering = null; | |
if (!string.IsNullOrEmpty(order)) | |
{ | |
ordering = Ordering.By(order, direction, null, false); | |
} | |
return Umbraco.Core.Composing.Current.Services.ContentService.GetPagedChildren(parent.Id, 0, top, out total, filter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment