首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LINQ中的递归/分层查询

LINQ中的递归/分层查询
EN

Stack Overflow用户
提问于 2016-08-17 11:13:17
回答 1查看 745关注 0票数 1

场景是,

我使用EntityFrame的工作6。我有一个数据库有表的结构如下。

代码语言:javascript
复制
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

代码语言:javascript
复制
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()

请看下面的完整代码,它用在了inside LINQ query的select子句中

代码语言:javascript
复制
 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>();

帮帮我,提前谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-17 16:43:31

试试这个:

代码语言:javascript
复制
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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38987524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档