递归/分层查询在LINQ

本文关键字:LINQ 查询 分层 递归 | 更新日期: 2023-09-27 18:02:52

场景为,

我使用entityframework6。我有一个DB表,下面有结构。

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

表有关系形式的层次数据,如您所见。

我想遍历这个层次结构,想要特定的父级,如果我在geo_id 6,那么我想去parent_id 3并获得值Attock,或者想去parent_id 2并获得值Punjab

这个故事的寓意是,站在任何一个子节点上,都要遍历到指定的父节点或祖父母节点,而不是整个层级。下面是我尝试过的代码,但它只给了我它的直接父节点。

更简单地说,想要一个LINQ查询,它将返回指定父或祖父母的名称,例如。我可以问我的问题,"嘿!我是Hasan Abdal(City),告诉我我的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()

参见下面的完整代码,它用于LINQ查询的select子句

 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>();

帮帮我,提前谢谢

递归/分层查询在LINQ

试试这个:

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"