score:1
Since you are always going to be returning ALL of the items in the table, why not just make a recursive method that gets all children for a parent and then use that on the in-memory Items:
partial class Items
{
public IEnumerable<Item> GetAllChildren()
{
//recursively or otherwise get all the children (using the Hierarchy navigation property?)
}
}
then:
var items =
from item in Items.ToList()
group new
{
item.itemID,
item.GetAllChildren()
} by item.itemID;
Sorry for any syntax errors...
score:0
Well, if the hierarchy is strictly 2 levels you can always union them and let LINQ sort out the SQL (it ends up being a single trip though it needs to be seen how fast it will run on your volume of data):
var hlist = from h in Hierarchies
select new {h.Parent, h.Child};
var slist = from h in Hierarchies
join h2 in hlist on h.Parent equals h2.Child
select new {h2.Parent, h.Child};
hlist = hlist.Union(slist);
This gives you an flat IEnumerable<{Item, Item}>
list so if you want to group them you just follow on:
var glist = from pc in hlist.AsEnumerable()
group pc.Child by pc.Parent into g
select new { Parent = g.Key, Children = g };
I used AsEnumerable()
here as we reached the capability of LINQ SQL provider with attempting to group a Union. If you try it against IQueryable it will run a basic Union for eligable parents then do a round-trip for every parent (which is what you want to avoid). Whether or not its ok for you to use regular LINQ for the grouping is up to you, same volume of data would have to come through the pipe either way.
EDIT: Alternatively you could build a view linking parent to all its children and use that view as a basis for tying Items. In theory this should allow you/L2S to group over it with a single trip.
Source: stackoverflow.com
Related Articles
- LINQ Source Code Available
- Recursive Linq Grouping
- creating Linq to sqlite dbml from DbLinq source code
- How to convert this recursive code to Linq
- source code for LINQ 101 samples
- convert recursive code to LINQ
- LINQ Recursive query in web api code
- c# Linq or code to extract groups from a single list of source data
- What would be the Linq code for this grouping using VB.NET 2008?
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- Linq - Grouping by date and selecting count
- Recursive Hierarchy - Recursive Query using Linq
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- C# Linq Grouping
- linq to sql recursive query
- Syntax to execute code block inside Linq query?
- Recursive LINQ calls
- Recursive LINQ query: select item and all children with subchildren
- Linq extension method, how to find child in collection recursive
- Complex Linq Grouping
- LINQ Grouping dynamically
- Get sum of two columns in one LINQ query without grouping
- LINQ to SQL - Grouping by hours
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Linq recursive parent child
- Is there a good source that gives an overview of linq optimizations?
- Expression Trees on DataTable
- Replace a JSON value of a property in several JObjects
- Comparing 2 lists using Linq
- LINQ Select and Convert Entites to ViewModels
- Linq to Entities, .Take() on VARBINARY field results in ArgumentException 'DbLimitExpression requires a collection argument
- IQueryable Sort not working
- How to Filter DataGridView when bound to a linq query result that is linked to an EF4 Entity
- Get all objects from a tree with a particular property using Linq
- How to convert linq expression to dictionary<string,string>
- LINQ query - needs to change a field name
- Linq count vs IList count
- search an array of string in a large string and check if any exist using linq
- How can I select distinct and ordered data into a generic list from multiple lists?
- Complex SQL to LINQ to populate Grid (Count items year by month) (MVC, EF Code First)
- Avoid magic number when looking for min value
- Unable to Parse XML using LINQ in ASP.Net & C#
- Check if column exists in linq
- LINQ: Link 1:M Table and Merge Results
- Lambda expression where id in list of ids
- Simple XML parsing throws Object reference error