score:1
As mentioned in comments, currently it's not possible to create a recursive expandable (i.e. non invokable) expression.
However, if you can limit the maximum depth, one possible solution would be to build expression like this (utilizing the EF navigation property):
Parents = new MyTable [] { x.Parent, x.Parent.Parent, x.Parent.Parent.Parent, ...}
.Where(e => e != null)
dynamically:
static Expression<Func<MyTable, IEnumerable<MyTable>>> ParentsSelector(int maxLevels)
{
var parameter = Expression.Parameter(typeof(MyTable), "x");
var parents = new Expression[maxLevels];
for (int i = 0; i < parents.Length; i++)
parents[i] = Expression.Property(i > 0 ? parents[i - 1] : parameter, "Parent");
Expression<Func<MyTable, bool>> predicate = x => x != null;
var result = Expression.Call(
typeof(Enumerable), "Where", new[] { parameter.Type },
Expression.NewArrayInit(parameter.Type, parents), predicate);
return Expression.Lambda<Func<MyTable, IEnumerable<MyTable>>>(result, parameter);
}
and use it as follows:
var parents = ParentsSelector(10);
var query = dbContext.MyTable
.AsExpandable()
.Where(x => x.Id == myId)
.Select(x => new
{
Parents = parents.Invoke(x)
});
Source: stackoverflow.com
Related Query
- StackOverflowException using Linq(Kit) for nested data
- Using LINQ query result for data source for GridControl c#
- How to design nested query for fetching data from tables of MS SQL using Entity Framework?
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to reuse a linq expression for 'Where' when using multiple source tables
- What would be a good approch for using XML as data persistence for a small C# app?
- Using LINQ for CSV data
- Using ToList() on Enumerable LINQ query results for large data sets - Efficiency Issue?
- Best Practice for retrieving data using Entity Framework 4.0
- Using Linq to Entity for searching large data cause high memory usage
- read icollection data using LINQ in C# code
- Are we compromising performance for achieving code readability when using LINQ?
- Slow speed when using nested loops when comparing large data sets
- LINQ query help: searching for data in a Many to Many using Entity Framework
- Code Rewite for tuple and if else statements by using LINQ
- Dynamic Linq using Data Objects. How to convert Int32 to String for purpose of calling String.Contains()
- Can I filter children rather than nested for loop using LINQ
- How to filter related data using Entity Framework and LINQ to SQL and LinqKit PredicateBuilder Or IdeaBlade DevForce
- C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source
- Better ways for merging data from two sources using Linq
- Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?
- Using linq to group by dates with values for empty data
- Nested sorting for predefined filters using LINQ and RavenDB
- A better way than having 3 nested for loops to get data I need
- replace 3 levels of nested for loops by efficient code possibly linq
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
- Searching for an item in a list that's nested inside another list based on a property value in C# using LINQ?
- Using LINQ nested query get latest rows for update
- Linq fill data for nested List<T>
- Find data for latest Inserted date using linq in c#, mysql
More Query from same tag
- Modifying an item in List
- c# Linq: Replace empty string with some value
- How to query the average from a certain property in an Entity and return it in a List of List of objects in LINQ?
- Linq updating different table after join process
- Using LINQ with Select and Distinct Methods
- Converting datatable into hierarchical data structure (JSON) using C#
- Avoiding OUTER APPLY in Entity Framework projections
- Why do I need LINQ if I use NHibernate-like ORMs?
- How can I intercept elements in an IQueryable regardless of how many transforms are applied?
- Add an entity into existing entity position C# EF
- How to implement Count method on Mocked IQueryable object
- Calculate sum of each row using LINQ
- How to order a list of paragraphs
- LINQ multiple joins with one left join
- LINQ: Get first character of each string in an array
- Split array into subarrays of specific length
- Use IEnumerable.Sum to subtract in LINQ
- Net Core: Entity Framework ThenInclude with Projection Select
- Linq SUM on objects?
- Linq to CSV select by column value
- How to sort a list with the values of another list with Linq
- Saving data to database using linq
- How do I find duplicate sequential entries in a Generic List in C#?
- Linq Query For Many to Many Relation within Intersecting Table
- C# algorithm refactor splitting an array into 3 parts?
- Explicit construction of entity type '' in query is not allowed
- Getting object with max date time value in one of it's properties
- LINQ suming nested groups in one statement
- Deleting rows in a table with Linq to SQL
- How to add more query on to the end of an IEnumerable