求一 Linq

LQknife 2009-07-01 11:38:07
初学Linq,找人帮忙^_^!
/*
* 场景:有一Panel控件,这个控件中嵌套了一个或多个TabControl控件,TabControl控件有
* 一个或多个TabPage,最多3层嵌套TabPage.Parent -> TabControl, TabControl.Parent->Panel
* Panel中还有一些其它控件不过这些控件都不会再有子控件
*
* 需求:从这个Panel控件中找出它的子控件,这些子控件不能再有子控件,如果这个子控件是
* TabControl找出这个TabControl中所有TabPage中的子控件(也就是Panel的重孙控件)
*
* 要求:一句Linq搞定
*/
...全文
108 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LQknife 2009-07-01
  • 打赏
  • 举报
回复
咋没有将帖子提前功能了?
LQknife 2009-07-01
  • 打赏
  • 举报
回复
沙发 我自个写的
var query = from c in this.Control.Controls.Cast<Control>()
let ctrls1 = c.Controls.Cast<Control>()
from c1 in ctrls1
let ctrls2 = c1.Controls.Cast<Conltrol>()
where !c.HasChildren && !(c is UCode)
from c2 in ctrls2
但是最后的select不知道怎么把 c 和 c2 连接起来了
对了 再加个要求Linq返回的序列必须是 IEnumrable<Control>
baiyunyinv 2009-07-01
  • 打赏
  • 举报
回复

var controls = (from Control c in panel1.Controls
where !c.HasChildren
select c).Union(from Control tc in panel1.Controls
let tabPages = tc.Controls.Cast<Control>()
from Control tp in tabPages
let tps = tp.Controls.Cast<Control>()
from tpCtrl in tps
where tc is TabControl
select tpCtrl);

借用。。。。
LQknife 2009-07-01
  • 打赏
  • 举报
回复
其实我的初衷是不想遍历 panel.Controls两次
难道一次遍历搞不定了吗
PENYY 2009-07-01
  • 打赏
  • 举报
回复
mark
我姓区不姓区 2009-07-01
  • 打赏
  • 举报
回复

var controls = (from Control c in panel1.Controls
where !c.HasChildren
select c).Union(from Control tc in panel1.Controls
let tabPages = tc.Controls.Cast<Control>()
from Control tp in tabPages
let tps = tp.Controls.Cast<Control>()
from tpCtrl in tps
where tc is TabControl
select tpCtrl);
feifeiyiwen 2009-07-01
  • 打赏
  • 举报
回复
up

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧