Using Linq to SQL, how can I load all children and any nested children

I have 5 tables in L2S dbml classes: Global -> Categories -> ItemType -> Item -> ItemData. In the example below, I only reached item item.

    //cdc is my datacontext

DataLoadOptions options = new DataLoadOptions();

options.LoadWith<Global>(p => p.Category);
options.AssociateWith<Global>(p => p.Category.OrderBy(o => o.SortOrder));
options.LoadWith<Category>(p => p.ItemTypes);
options.AssociateWith<Category>(p => p.ItemTypes.OrderBy(o => o.SortOrder));

cdc.LoadOptions = options;

TraceTextWriter traceWriter = new TraceTextWriter();
cdc.Log = traceWriter;

var query =
from g in cdc.Global
where g.active == true && g.globalid == 41
select g;

var globalList = query.ToList();

// In this case I have hardcoded an id while I figure this out
// but intend on trying to figure out a way to include something like globalid in (#,#,#)
foreach (var g in globalList)
{

   // I only have one result set, but if I had multiple globals this would run however many times and execute multiple queries like it does farther down in the hierarchy 
    List<Category> categoryList = g.category.ToList<Category>();

    // Doing some processing that sticks parent record into a hierarchical collection

    var categories = (from comp in categoryList
        where comp.Type == i 
        select comp).ToList<Category>();

    foreach (var c in categories)
    {
        // Doing some processing that stick child records into a hierarchical collection
        // Here is where multiple queries are run for each type collection in the category
        // I want to somehow run this above the loop once where I can get all the Items for the categories
        // And just do a filter

        List<ItemType> typeList = c.ItemTypes.ToList<ItemType>();

        var itemTypes = (from cat in TypeList
                where cat.itemLevel == 2
                select cat).ToList<ItemType>();

        foreach (var t in itemTypes)
        {
           // Doing some processing that stick child records into a hierarchical collection                            
        }
    }
}

"List typeList = c.ItemTypes.ToList ();"
This line is executed multiple times in foreach, and the query is executed to get the results, and I understand why to some extent, but I thought that it would reliably load Loadwith as an option, as in the case of fetching with just one query.

, , L2S "" , , " ", . , . , itemtype . - ( * ItemTypes Where CategoryID ( categoryID , GlobalID (#, #, #))

, , , , , , , , , .

+5
2

Linq to SQL .

Linq To SQL . , - ( ) , . , . LINQ to SQL log (n), n - . ToList , , .

.:

http://www.cnblogs.com/cw_volcano/archive/2012/07/31/2616729.html

+4

, , , . . , , L2S, - , .

    var query =
    from g in cdc.Global
    where g.active == true && g.globalId == 41
    select g;

    var globalList = query.ToList();

    List<Category> categoryList = g.category.ToList<Category>();

    var categoryIds = from c in cdc.Category
                where c.globalId == g.globalId
                select c.categoryId;

    var types = from t in cdc.ItemTypes
                where categoryIds.Any(i => i == t.categoryId)
                select t;

    List<ItemType> TypeList = types.ToList<ItemType>();

    var items = from i in cdc.Items
                from d in cdc.ItemData
                where i.ItemId == d.ItemId && d.labelId == 1
                where types.Any(i => i == r.ItemTypes)
                select new 
                    {
                        i.Id, 
                        // A Bunch of more fields shortened for berevity
                        d.Data    
                    };

    var ItemList = items.ToList();

    // Keep on going down the hierarchy if you need more child results
    // Do your processing psuedocode
    for each item in list
        filter child list
        for each item in child list
            .....
    // 

, , , .

0

All Articles