14 回答
TA贡献1784条经验 获得超2个赞
int GetItemIndex(int index, IDictionary<int, IList<int>> t) { var result = from x in t where x.Value.Contains(index) select x.Value.IndexOf(index); if (result == null || result.Count() < 1) return -1; return result.FirstOrDefault(); }
这个其实真不用那么复杂
TA贡献1825条经验 获得超4个赞
@笨笨蜗牛: 分支根据二级目录来的,二级目录共有三个分支,1下面到三级目录的值有5, 6, 7, 8, 20, 13, 24和23; 2下面有22, 25, 26, 27; 3下面有19, 28, 29, 30, 31, 38, 33和 32, 35和 34, 36, 37,
这样分成三组数据,给定一个值,求所在分组的index
TA贡献1829条经验 获得超13个赞
@happydaily: 刚写了下,太复杂了。
下面的代码是分条写的:
int value = 3; var k = from c in thirdlevel where c.Value.Contains(value) select new { k = c.Key, index = c.Value.IndexOf(value) }; var r = k.SingleOrDefault() ?? new { k = 0, index = 0 }; var m = (from c in secondlevel where c.Value.Contains(r.k) select c.Value).Single(); var n = m.Where(o => m.IndexOf(o) < m.IndexOf(r.k)).Sum(o => thirdlevel[o].Count); var index = n + r.index;
如果要写在一起也不是不可以,但那个编写过程很头痛,而且理解起来也会很难。
TA贡献1813条经验 获得超2个赞
@笨笨蜗牛: 这个是另外一个版本的,整合到了一起,不过中间还是有几个地方感觉没简化好,需要再斟酌,可惜没那感觉了。
var q = from c in thirdlevel from d in secondlevel where c.Value.Contains(value) && d.Value.Contains(c.Key) select c.Value.IndexOf(value) + d.Value .Where(o=>d.Value.IndexOf(o) < d.Value.IndexOf(c.Key)) .Sum(o=>thirdlevel[o].Count); var index = q.Single();
TA贡献1786条经验 获得超13个赞
int i=5;
var second = from m in secondlevel
where m.Value.Contains((from n in thirdlevel
where n.Value.Contains(i)
select n).FirstOrDefault().Key)
select m;
TA贡献2037条经验 获得超6个赞
int i = 5; var second = from m in secondlevel let s=m.Value.IndexOf((from n in thirdlevel where n.Value.Contains(i) select n).FirstOrDefault().Key) where m.Value.Contains((from n in thirdlevel where n.Value.Contains(i) select n).FirstOrDefault().Key) select s;
@無限遐想:
TA贡献1872条经验 获得超3个赞
@無限遐想: 分支根据二级目录来的,二级目录共有三个分支,1下面到三级目录的值有5, 6, 7, 8, 20, 13, 24和23; 2下面有22, 25, 26, 27; 3下面有19, 28, 29, 30, 31, 38, 33和 32, 35和 34, 36, 37,
这样分成三组数据,给定一个值,求所在分组的index
- 14 回答
- 0 关注
- 412 浏览
添加回答
举报