簡體   English   中英

LINQ中的遞歸/分層查詢

[英]Recursive/Hierarchical Query in LINQ

情景是,

即時通訊使用EntityFrame工作6.我有一個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查詢,它將返回指定的父或祖父的名稱,例如。 我可以問我的查詢,“哎!IM 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>();

幫幫我,謝謝你

嘗試這個:

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM