score:4
Accepted answer
you can try with recursion
public static list<locationviewmodel> gethierarchy(list<linkparentchildviewmodel> linkparentchildviewmodels, int parentid)
{
return linkparentchildviewmodels.where(x => x.parent.id == parentid).select(x => new locationviewmodel
{
id = x.parent.id,
code = x.parent.code,
childlocations = gethierarchy(linkparentchildviewmodels, x.child.childlocationid)
}).tolist();
}
call this from main method
var result = gethierarchy(linksparentchild, 8);
Source: stackoverflow.com
Related Query
- LINQ query to group parent and child elements
- Linq Query to group Parent and respective child rentities
- How to combine LINQ group and ordering with parent and child tables?
- Linq query to orderby level then parent and child item
- Parent and Child fluent Linq query in C#?
- LINQ Query for outerjoining Parent and child (nested) tables
- How to write an efficient LINQ query when searching by both parent and child entity fields
- linq query to get all child elements by passing parent id
- Load parent and child table in one query linq to entitiy
- linq lamba how can i query a list of child properties and return the parent
- Using LINQ on local squence to group by parent and child properties
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value
- Linq lambda query to select child and specify parent data
- Group multiple child tables by a parent table in a single LINQ query
- How to extract child groups from parent and re group using Linq
- Linq Query Group By and Selecting First Items
- Take the first five elements and the last five elements from an array by one query using LINQ
- Entity Framework - Linq query with order by and group by
- LINQ Query with GROUP and SUM
- Simple sql to Linq query with group by and aggregate functions
- Translating query with GROUP BY and COUNT to Linq
- Linq group by parent property order by child
- LINQ query with GROUP BY and Count(*) into Anonymous Type
- Linq query using group by and having
- Joining parent and child collection in LINQ for a single merged output
- Linq projection of flattened table into parent and child object graph
- Linq query to return a flatened list of parent child
- How do I order the elements in a group by linq query, and pick the first?
- Optimize/rewrite LINQ query with GROUP BY and COUNT
- Linq order by parent and child
More Query from same tag
- How to create a list of one object attributes from the actual list of objects?
- C# Linq add elements from one list<custom> to another comparing them and changing values
- How to return a date or an error? c# .Net
- Doing multiple joins within a LINQ statement
- How to use contain clause to query multiple parameters?
- C# cast to another class with recursion
- How to do I convert int to string in linq query
- LINQ to create new records in a XML file
- LINQ order by null column where order is ascending and nulls should be last
- Entity framework Linq very slow queries
- IntelliSense for XElement objects with XML schema
- Different DataContext error. Why is this a problem?
- Linq XML complex select (3 levels)
- How to convert groupby query to linq and compute other thing while executing?
- Query with Nullable Values using LINQ
- LINQ - Output true/false "checklist" of items from list A contained in list B
- LINQ QueryProvider Select
- How do I get Distinct() to work with a collection of custom objects
- Copy a List<SomeClass> into other but applying a function to a property in LINQ
- EF6 using custom property in a linq query
- LinQ optimization
- 'int' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains
- GroupJoin and Include
- How to remove duplicates from a list of nested objects?
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- Setting a datatable cell value using LINQ according to some conditions
- C# Select Overload Method With Accumulator Not Working
- Search list and order List by value max found c#
- ControlCollection extension method optimization
- Lambda Expression: How to map to a List rather than IEnumerable?