简体   繁体   中英

Recursive/Hierarchical Query in LINQ

Scenario is,

im using EntityFrame Work 6. I have a DB have table having structure below.

cat_id     geo_id     parent_id geo_name
   Root       1       NULL      Pakistan
   Province   2        1        Punjab
   District   3        2        Attock
   City       4        3        Attock
   City       5        3        Fateh Jang
   City       6        3        Hasan Abdal

Table have hierarchical data in relational form,as you can see.

I want to traverse this hierarchy, want to specific parent level, If im at geo_id 6 then I want to go parent_id 3 and get value Attock or want to go to parent_id 2 and want to get value Punjab .

Moral of the story is,standing at any child,want traverse till specified parent or grand parent,not entire hierarchy. Below is code I have tried but it gives me only its immediate parent.

More Shortly, Want a LINQ query,which will return name of specified parent or grand parent,For Example. I can ask my query, "hey! im Hasan Abdal(City) ,tell me my Province "

Province = (from cp in db.geo_hierarchy
                                                     join mp in db.geo_hierarchy on cp.parent_id equals mp.geo_id
                                                     where cp.geo_name == risk.geo_hierarchy.geo_name 
                                                     select mp.geo_name).FirstOrDefault()

see full code below,it used in inside LINQ query's select clause

 Risklists = (from risk in db.risk_cat_detail
            where risk.occurance_date.Value.Year==2014 && risk.occurance_date.Value.Month>=6 && risk.occurance_date.Value.Month<=9
                                 select new risk_cat_detail_contract()
                                 {
                                     cat_id = risk.cat_id,
                                     catdesc = risk.category_main.cat_name,
                                     risk_cat_detail_id = risk.risk_cat_detail_id,
                                     trans_date = risk.trans_date.Value,
                                     occurance_date = risk.occurance_date.Value,
                                     occurance_time = risk.occurance_time,

                                     geo_id = risk.geo_id,
                                     geo_desc = risk.geo_hierarchy.geo_name,
                                     Province = (from cp in db.geo_hierarchy
                                                 join mp in db.geo_hierarchy on cp.parent_id equals mp.geo_id
                                                 where cp.geo_name == risk.geo_hierarchy.geo_name 
                                                 select mp.geo_name).FirstOrDefault()







                                 }).ToList<risk_cat_detail_contract>();

Help me out,Thanks in Advance

Try this:

var geo_hierarchy = new []
{
    new { cat_id = "Root", geo_id = 1, parent_id = (int?)null, geo_name = "Pakistan", },
    new { cat_id = "Province", geo_id = 2, parent_id = (int?)1, geo_name = "Punjab", },
    new { cat_id = "District", geo_id = 3, parent_id = (int?)2, geo_name = "Attock", },
    new { cat_id = "City", geo_id = 4, parent_id = (int?)3, geo_name = "Attock", },
    new { cat_id = "City", geo_id = 5, parent_id = (int?)3, geo_name = "Fateh Jang", },
    new { cat_id = "City", geo_id = 6, parent_id = (int?)3, geo_name = "Hasan Abdal", },
};

var map = geo_hierarchy.ToDictionary(x => x.geo_id);

Func<int, string, string> up = null;
up = (geo_id, cat_id) =>
{
    var record = map[geo_id];
    return
        record.cat_id == cat_id
            ? record.geo_name
            : (record.parent_id.HasValue
                ? up(record.parent_id.Value, cat_id)
                : null);
};

var result = up(5, "Province"); // -> "Punjab"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM