首页 新闻 会员 周边

求一个简单的LINQ语句

0
悬赏园豆:10 [已解决问题] 解决于 2012-05-10 15:09
IDictionary<int, IList<int>> secondlevel = new Dictionary<int, IList<int>>();
IDictionary<int, IList<int>> thirdlevel = new Dictionary<int, IList<int>>();
 secondlevel.Add(1, new List<int>(new int[] { 1, 2 }));
 secondlevel.Add(2, new List<int>(new int[] { 3 }));
 secondlevel.Add(3, new List<int>(new int[] { 4, 5, 6 }));
 thirdlevel.Add(1, new List<int>(new int[] { 5, 6, 7, 8, 20, 13, 24 }));
 thirdlevel.Add(2, new List<int>(new int[] { 23 }));
 thirdlevel.Add(3, new List<int>(new int[] { 22, 25, 26, 27 }));
 thirdlevel.Add(4, new List<int>(new int[] { 19, 28, 29, 30, 31, 38, 33 }));
 thirdlevel.Add(5, new List<int>(new int[] { 32, 35 }));
 thirdlevel.Add(6, new List<int>(new int[] { 34, 36, 37 }));

定义了一个二级目录和三级目录,二级目录的LIST<INT>值就是三级目录的keys,现在给定一个三级目录的值,比如22,如何得到二级目录对应的key,结果应该是2

happydaily的主页 happydaily | 菜鸟二级 | 园豆:301
提问于:2012-05-10 09:24
< >
分享
最佳答案
0
int GetMenuKey(int index, IDictionary<int, IList<int>> s, IDictionary<int, IList<int>> t)
        {
            int menuKey = t.Where(x => x.Value.Contains(index)).FirstOrDefault().Key;
            return s.Where(x => x.Value.Contains(menuKey)).FirstOrDefault().Key;
        }

没有检查没有匹配项的情况,需要注意一下

收获园豆:10
sinhbv | 老鸟四级 |园豆:2579 | 2012-05-10 09:33
其他回答(1)
0

你确定值应该是2么。。。不是3么。。。

var key = secondlevel.Keys.Single(k => thirdlevel[k].Contains(22));

前提是你的树形结构构造是正确的。

水牛刀刀 | 园豆:6350 (大侠五级) | 2012-05-10 10:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册