score:2
i think the best i'm going to get is to load the entire hierarchy in one hit from the structure i want the top parent of:
var structurequery = from item in context.structures
.include(x => x.parent)
where item.structureid == structureid
select item;
then just use the code:
while (structure.parent != null)
{
structure = structure.parent;
}
score:-4
you can use the linq take construct, for instance
var first3customers = (
from c in customers
select new {c.customerid, c.customername} )
.take(2);
score:-1
i like the question and can't think of a linq-y way of doing this. but could you perhaps implement this on your repository class? after all, there should be only one at the top and if the need for it is there, then maybe it deserves a structurerepository.getroot()
or something.
score:0
i have a similar situation. i didn't manage to solve it directly with linq/ef. instead i solved by creating a database view using recursive common table expressions, as outlined here. i made a user-defined function that cross applies all parents to a child (or vice versa), then a view that makes use of this user-defined function which i imported into my ef object context.
(disclaimer: simplified code, i didn't actually test this)
i have two tables, say mytable (containing all items) and myparentchildtable containing the childid,parentid relation
i have then defined the following udf:
create function dbo.fn_getsupertree(@childid as int)
returns @tree table
(
childid int not null
,parentid int null
,level int not null
)
as
begin
with parent_tree(childid, parentid)
as
(
-- anchor member (am)
select childid, parentid, 0
from myparentchildtable
where childid = @childid
union all
-- recursive member (rm)
select info.childid, info.parentid, tree.[level]+1
from myparentchildtable as info
join parent_tree as tree
on info.childid = tree.parentid
)
insert into @tree
select * from parent_tree;
return
end
and the following view:
create view vwsupertree as (
select tree.*
from mytable
cross apply fn_getsupertree(mytable.id) as tree
)
go
this gives me for each child, all parents with their 'tree level' (direct parent has level 1, parent of parent has level 2, etc.). from that view, it's easy to query the item with the highest level. i just imported the view in my ef context to be able to query it with linq.
score:2
this is not a direct answer, but the problem you are having is related to the way you are storing your tree. there are a couple ways of simplifying this query by structuring data differently.
one is to use a nested set hierarchy, which can simplify many kinds of queries across trees.
another is to store a denomralized table of ancestor/descendant/depth tuples. this query then becomes finding the tuple with the current structure as the descendant with the maximum depth.
Source: stackoverflow.com
Related Query
- Is there a way, using LINQ/EF, to get the top most item in a parent/child hierarchy?
- Using LINQ to get one entity with the most of a child entity
- If you have a LINQ statement using the Select() method, is there a way to get a value from the next record?
- What's the most efficient way to get only the final row of a SQL table using EF4?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Is there a way to get the SQL created by a LINQ query?
- Get array index values of the top 1000 largest entries inside an array using LINQ
- Using LINQ to get a list of items where the item contains a part of an item from another list
- How to get the exact child element with corresponding parent only from multilevel embedded MongoDB document using C#
- get parent name and child count from model using LINQ
- How can I get the top three players and their high scores from a collection using linq and lambdas
- LINQ to objects: Is there a way to pass to LINQ the accessor from which to get values?
- Is there any benefit to using LINQ to get the first (and only) element in IEnumerable<T>?
- How to get the second repeated item from a collection of objects using LINQ to object
- LINQ to SQL - is there a way to get a 'preview' of the database with changes made to a context?
- Efficient way to be updating child records when updating the Master table using Linq
- Using First() get the 2nd item of LINQ result?
- Using Linq lambdas, how can I get the first item in a two-key-sorted list?
- using LINQ to get the top N percent of a list
- Get the index of a partially matching item in a list c# using linq
- Most efficient way to get MAX value of joined table in LINQ query using C#
- What is the most compact way using LINQ to check whether an IEnumerable<int> is ordered?
- How to Filter Child Collection from each Parent Entity in the Parent Collection using Linq
- How to get the maximum item ordered by two fields using a Linq expression?
- Is there a way in linq wherin i can insert a row(from dictionary) in datatable using the list of column names c#?
- Use LINQ to get only the most recent JOINed item for each element
- How to get the Distinct item using linq
- Using LINQ to switch the parent key with the child keys of a two dimensional dictionary
- Is there a way to get the long value from a Date in a Linq to Entity query?
- How to render related items in a child ListView using the item ID from the parent
More Query from same tag
- Filter duplicates from list
- Sorting with LINQ
- converting from linq var result to another object
- Debugging LINQ Queries
- Linq select all numbers that are in any interval from another list of intervals
- How Can I Select One or More column Asp.Net Entity Framework
- How can I .Include down more than one level in LINQ?
- Projecting object with LINQ to XML but want empty string instead of NULL
- LINQ group by sum not working as expected
- Can anyone tell me if this is possible to do in Linq?
- System.IObservable<string> to System.IObservable<System.Collections.Generic.List<string>>
- Entity Framework DBContext global caching?
- Find elements in a list that, together add up to a target number
- Left outer join with null LINQ exception with Guids
- Export an IQueryable Collection to Excel
- Is it possible to get the original type of a list when all you have is a "WhereEnumerableIterator" of that list?
- Skip first column and get distinct from other columns
- Separate groups of objects based on their properties
- Randomly Select Records Using Linq to SQL
- Why XDocument encoding type changed while use XDocument's WriteTo method
- How to cast instance of Enumerable<T>.Where(Predicate) to IEnumerable<T>?
- How to Left Outer Join two DataTables in c#?
- How to order by C# list so that the duplicate items appear in the reverse order of their insertion
- Default value for linq select item if query didn't return anything
- LINQ query to use right key for the correct keyvaluepair
- Linq: how to get values of second level nodes
- Generic method using Linq and inheritance to bind to object
- Return select objects and only desired subvalues from a list of lists using LINQ
- Linq: Sorting by irregular order
- Groupby list within the list using LINQ