LINQ is not intended to handle recursive selection.
Of course, you can write your own extension method to compensate for this in LINQ for objects, but I found that LINQ to Entities does not like functionality that is not easily translated into SQL.
Edit:
Oddly enough, LINQ to Entities is not complaining that Matt Warren will take recursion using LINQ here . You can do:
var result = db.Table.Where(item => item.GroupId == 5)
.Traverse(item => db.Table.Where(parent
=> item.ParentId == parent.GroupId));
using the extension method defined here:
static class LinqExtensions
{
public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source,
Func<T,IEnumerable<T>> selector){
foreach(T item in source){
yield return item;
IEnumerable<T> children = selector(item);
foreach (T child in children.Traverse(selector))
{
yield return child;
}
}
}
Perhaps crowding out may be unsatisfactory.
source
share